简体   繁体   中英

Find min/max of complex 2D array

PHP is not usually my language so please bear with me.

I have a (in my opinion) poorly designed 2D array of product categories and I would like to get the min/max of the two ratings per sub-category. Sadly, I cannot change the layout of the array:

[[Category, SubCategory, OverallRating, ExtraRating]]

For example, some sample data is like this:

[["Fridges",         "Samsung", 5, 6],
 ["Fridges",         "Samsung", 2, 1],
 ["Fridges",         "Samsung", 3, 4],
 ["Fridges",         "LG",      7, 5],
 ["Washing Machine", "Letto",   5, 6],
 ["Washing Machine", "Samsung", 5, 6],
 ["Fridges",         "Samsung", 4, 4]]

The output of this should give me data such that:

Fridges, Samsung: 2/5, 1/6
Fridges, LG: 7/7, 5/5 (or just 7, 5)
Washing Machine, Letto: 5, 6 (see above)
Washing Machine, Samsung: 5, 6 (see above)

try this

<?php

$your_array = array(array("Fridges", "Samsung", 5, 6), array("Fridges", "Samsung", 2, 1), array("Fridges", "Samsung", 3, 4), array("Fridges", "LG", 7, 5), array("Washing Machine", "Letto", 5, 6), array("Washing Machine", "Samsung", 5, 6), array("Fridges", "Samsung", 4, 4));

$arr_temp = array();
foreach($your_array as $key=>$arr)
{
     $category = $arr[0];
     $sub_category = $arr[1];
     $overall_rating = $arr[2];
     $extra_rating = $arr[3];

     if(isset($arr_temp[$category][$sub_category]['overall']))
     {
        $overall_min =  $arr_temp[$category][$sub_category]['overall']['min'];
        $overall_max =  $arr_temp[$category][$sub_category]['overall']['max'];

        if($overall_rating<$overall_min)
        {
            $arr_temp[$category][$sub_category]['overall']['min'] = $overall_rating;
        }
        if($overall_rating>$overall_max)
        {
            $arr_temp[$category][$sub_category]['overall']['max'] = $overall_rating;
        }

     }
     else
     {
        $arr_temp[$category][$sub_category]['overall']['min'] = $overall_rating;
        $arr_temp[$category][$sub_category]['overall']['max'] = $overall_rating;
     }


     if(isset($arr_temp[$category][$sub_category]['extra']))
     {
        $extra_min =  $arr_temp[$category][$sub_category]['extra']['min'];
        $extra_max =  $arr_temp[$category][$sub_category]['extra']['max'];

        if($extra_rating<$extra_min)
        {
            $arr_temp[$category][$sub_category]['extra']['min'] = $extra_rating;
        }
        if($extra_rating>$extra_max)
        {
            $arr_temp[$category][$sub_category]['extra']['max'] = $extra_rating;
        }

     }
     else
     {
        $arr_temp[$category][$sub_category]['extra']['min'] = $extra_rating;
        $arr_temp[$category][$sub_category]['extra']['max'] = $extra_rating;
     }


}

foreach($arr_temp as $category=>$arr1)
{
    foreach($arr1 as $sub_category=>$arr2)
    {
        echo $category.",".$sub_category.":".$arr2['overall']['min']."/".$arr2['overall']['max'].",".$arr2['extra']['min']."/".$arr2['extra']['max'];
        echo "\n";
    }
}
?>

OUTPUT :

Fridges,Samsung:2/5,1/6
Fridges,LG:7/7,5/5
Washing Machine,Letto:5/5,6/6
Washing Machine,Samsung:5/5,6/6

DEMO

How about this code?

Source:

 <?php function calculate($input) { $res = array(); foreach ($input as $data) { $key = $data[0].", ".$data[1]; $overall = $data[2]; $extra = $data[3]; if (isset($res[$key])) { // existing one if ($res[$key]["overall_min"] > $overall) { $res[$key]["overall_min"] = $overall; } if ($res[$key]["overall_max"] < $overall) { $res[$key]["overall_max"] = $overall; } if ($res[$key]["extra_min"] > $extra) { $res[$key]["extra_min"] = $extra; } if ($res[$key]["extra_max"] < $extra) { $res[$key]["extra_max"] = $extra; } } else { // new one $res[$key] = array( "key" => $key, "overall_min" => $overall, "overall_max" => $overall, "extra_min" => $extra, "extra_max" => $extra ); } } return $res; } // set input $input = [["Fridges", "Samsung", 5, 6], ["Fridges", "Samsung", 2, 1], ["Fridges", "Samsung", 3, 4], ["Fridges", "LG", 7, 5], ["Washing Machine", "Letto", 5, 6], ["Washing Machine", "Samsung", 5, 6], ["Fridges", "Samsung", 4, 4]]; // calculate $res = calculate($input); // print foreach ($res as $data) { echo sprintf( "%s: %d/%d, %d/%d<br />\\n", $data["key"], $data["overall_min"], $data["overall_max"], $data["extra_min"], $data["extra_max"] ); } ?> 

Output:

Fridges, Samsung: 2/5, 1/6 Fridges, LG: 7/7, 5/5 Washing Machine, Letto: 5/5, 6/6 Washing Machine, Samsung: 5/5, 6/6

Demo page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM