sandip kakade
sandip kakade

Reputation: 1356

Reduce an array to two elements after parsing and grouping the data

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 there any simple way to do this?

Upvotes: -1

Views: 371

Answers (2)

mickmackusa
mickmackusa

Reputation: 48031

I don't understand the context / business logic, but I can't provide a script to extract, group and flatten that data set. For functional-style programming, this is a job for array_reduce(). To parse the numbers into int-type values, use sscanf(), then conditionally push the extracted data into the result. Demo

var_export(
    array_reduce(
        $array,
        function ($result, $v) {
            if (sscanf($v, '%d - %d', $ints[], $ints[]) === 2) {
                $result[0] ??= [];
                array_push($result[0], ...$ints);
            } else {
                $result[1] = $ints[0];
            }
            return $result;
        },
        []
    )
);

Output:

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

Upvotes: 0

Georges O.
Georges O.

Reputation: 992

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.

Upvotes: 0

Related Questions