找到复杂2D阵列的最小值/最大值

时间:2014-08-18 06:20:42

标签: php arrays

PHP通常不是我的语言,所以请耐心等待。

我有(在我看来)设计不佳的2D产品类别数组,我想获得每个子类别的两个评级的最小值/最大值。可悲的是,我无法改变数组的布局:

[[Category, SubCategory, OverallRating, ExtraRating]]

例如,一些示例数据是这样的:

[["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]]

这个输出应该给我这样的数据:

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)

2 个答案:

答案 0 :(得分:0)

试试这个

<?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";
    }
}
?>

输出:

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

答案 1 :(得分:0)

这段代码怎么样?

<强>来源:

<?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"]
        );
    }
?>

<?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"] ); } ?>

<强>输出:

Demo page