Home > Back-end >  Create a price range from an array
Create a price range from an array

Time:09-25

I am trying to upper limit of the price filter should be based product prices

As you can see this o/p i am getting different price ranges for different products. I want to have just a single array in "options" key.

That will have highest upper limit for example

desired o/p =

[options] => Array
            (
                [0] => Array
                    (
                        [label] => 0-500
                        [value] => 0_500
                        [count] => // This will be total count
                    )

Current o/p

Array
(
    [label] => Price
    [count] => 5
    [attribute_code] => price
    [options] => Array
        (
            [0] => Array
                (
                    [label] => 0-100
                    [value] => 0_100
                    [count] => 2208
                )

            [1] => Array
                (
                    [label] => 100-200
                    [value] => 100_200
                    [count] => 338
                )

            [2] => Array
                (
                    [label] => 200-300
                    [value] => 200_300
                    [count] => 16
                )

            [3] => Array
                (
                    [label] => 300-400
                    [value] => 300_400
                    [count] => 1
                )

            [4] => Array
                (
                    [label] => 400-500
                    [value] => 400_500
                    [count] => 1
                )

        )

)

need guidance how it can be done ?

CodePudding user response:

Another version:

SUM [count]

array_column() : extract all count from $prices['options']

array_sum() : add them up.

$count = array_sum(array_column($prices['options'], 'count'));

EXTRACT all MIN and MAX values

array_map() : walk $prices['options'] and apply the given function

explode('_',$option['value']) :: split the string in a lower and upper part.

return the values to $minmax

$minmax = array_map(
   function($option){
     $split=explode('_',$option['value']);
     return [(int)$split[0],(int)$split[1]];
     },
   $prices['options']
 );

EXTRACT the MIN column from $minmax and get the lowest number: min()

$min=min(array_column($minmax,0));

EXTRACT the MAX column from $minmax and get the highest number: max()

$max=max(array_column($minmax,1));


$prices['options'] = [
    'label' => $min . '-' . $max,
    'value' => $min . '_' . $max,
    'count' => $count
];

An example

CodePudding user response:

Use array_sum(array_column($prices['options'], 'count')) to retrieve the total counts from each of the price options.

Use array_column($prices['options'], 'value'); to extract the values from the array, that can be used to sort or sum the values of.

Sort the prices by the values from lowest to highest using array_multisort($values, SORT_ASC, SORT_NATURAL, $prices['options']);

Extract the highest and lowest values from the sorted array, by using reset() and end() of the resulting sorted array.

Alternatively in PHP 7.3 use array_key_first() and array_key_last() respectively.

$values[array_key_first($values)];
$values[array_key_last($values)];

Use explode('_') on the highest and lowest values and retrieve the indexed offset of [0] or [1] of the first or last position of the separated values.

$counts = array_sum(array_column($prices['options'], 'count'));

$values = array_column($prices['options'], 'value');
array_multisort($values, SORT_ASC, SORT_NATURAL);

$lowest = explode('_', reset($values))[0];
$highest = explode('_', end($values))[1];

/* PHP 7.3 
$lowest = explode('_', $values[array_key_first($values)])[0];
$highest = explode('_', $values[array_key_last($values)])[1];
*/

$prices['options'] =  [
    'label' => $lowest . '-' . $highest,
    'value' => $lowest . '_' . $highest,
    'count' => $counts
];

var_export($prices['options']);

Result: https://3v4l.org/PmXg3
PHP 7.3 : https://3v4l.org/iTbcq

array (
  'label' => '0-500',
  'value' => '0_500',
  'count' => 2564,
)

Ambiguous Ranges

In the event you have ambiguous ranges, where the upper and lower bounds can be the same, EG: 100-200 and 100-600 or 100-600 and 200-600. Each lower and upper value would need to be extracted without needing to rely on sorting.

PHP 7.2 Example: https://3v4l.org/a8BCL#v7.2.34

$counts = array_sum(array_column($prices['options'], 'count'));

$values = [];
foreach ($prices['options'] as $priceOption) {
    [$values[], $values[]] = explode('_', $priceOption['value']);
}
$lower = min($values);
$upper = max($values);

$prices['options'] = [
    'label' => $lower . '-' . $upper,
    'value' => $lower . '_' . $upper,
    'count' => $counts,
];
  • Related