How to split array in php?

358 views Asked by At

I am little bit confused to get first and last value from array. And I tried to use explode()function but my logic is not working properly and very stupid logic.

My array

 Array
(
    [0] => 500 - 1112 
    [1] =>  1113 - 2224
    [2] => 2225 - 4446
    [3] => 4446 
)

I tried this way

$range = explode(',', $price_range);
        $count = count($range);
        if (1 == $count) {
            $price_1 = $range[0];
            $ranges['range1'] = explode(' - ', $price_1);
        } else if (2 == $count) {
            $price_1 = $range[0];
            $price_2 = $range[1];
            $ranges['range1'] = explode(' - ', $price_1);
            $ranges['range2'] = explode(' - ', $price_2);
        } else if (3 == $count) {
            $price_1 = $range[0];
            $price_2 = $range[1];
            $price_3 = $range[2];
            $ranges['range1'] = explode(' - ', $price_1);
            $ranges['range2'] = explode(' - ', $price_2);
            $ranges['range3'] = explode(' - ', $price_3);
        } else if (4 == $count) {
            $price_1 = $range[0];
            $price_2 = $range[1];
            $price_3 = $range[2];
            $price_4 = $range[3];
            $ranges['range1'] = explode(' - ', $price_1);
            $ranges['range2'] = explode(' - ', $price_2);
            $ranges['range3'] = explode(' - ', $price_3);
            $ranges['range4'] = explode(' - ', $price_4);
        }
     $array = call_user_func_array('array_merge', $ranges);
    sort($array);
    $min = reset($array);
    $max = end($array);

As per my array I want if in array getting single value in array for example

Array
(
    [0] => 500 - 1112 
    [1] =>  1113 - 2224
    [2] => 2225 - 4446
    [3] => 4446 
)

So I want to convert this array as shown below,

Array
(
    [0] => array(
        [0] => 500
        [1] => 1112 
        [2] => 1113 
        [3] => 2224
        [4] => 2225
        [5] => 4446
       )
    [1] => 4446 
)

And get min and max from Array ( [0] => array( from this array. Is their any simple way to do.

Thanks in advance

1

There are 1 answers

0
Georges O. On

If I correctly understand your example, you provided it with the parameter $count to 2.

So, this could be my version of your request:

The data

<?php
$data[] = '500 - 1112';
$data[] = '1113 - 2224';
$data[] = '4446';

The function

<?php
function explodeRanges(array $data, $counter, $explode = '-') {
    $return = [];

    // We take the correct number of rows
    foreach( array_slice($data, 0, $counter) as $value ) {
        $return = array_merge(
            $return,
            array_map('trim', explode($explode, $value))
        );
        // trim() function mapped on each elements to clean the data (remove spaces)
        // explode all values by the separator
    }
    return $return;
}

The output

<?php
for( $i = 1 ; $i <= 4 ; $i++ ) {
    $range = explodeRanges($data, $i);

    echo 'For ', $i, ' => [', implode(', ', $range), ']; MIN = ', min($range), '; MAX = ', max($range);
    echo '<hr />';
}

... and the result :)

For 1 => [500, 1112]; MIN = 500; MAX = 1112
For 2 => [500, 1112, 1113, 2224]; MIN = 500; MAX = 2224
For 3 => [500, 1112, 1113, 2224, 4446]; MIN = 500; MAX = 4446
For 4 => [500, 1112, 1113, 2224, 4446]; MIN = 500; MAX = 4446

If you need to repeat your code several times, it's because you can improve it. Here it's quick with a simple function.