从组数组中选择最大值

时间:2015-10-01 09:34:07

标签: php arrays arraylist

我有阵列:

Array
(
    [0] => Array
        (
            [tree_level] => 1
            [id] => 1
            [products_id] => 13
            [categories_id] => 1
        )
    [1] => Array
        (
            [tree_level] => 2
            [id] => 2
            [products_id] => 13
            [categories_id] => 2
        )
    [2] => Array
        (
            [tree_level] => 3
            [id] => 3
            [products_id] => 13
            [categories_id] => 3
        )
    [3] => Array
        (
            [tree_level] => 1
            [id] => 1
            [products_id] => 25
            [categories_id] => 1
        )
    [4] => Array
        (
            [tree_level] => 2
            [id] => 2
            [products_id] => 25
            [categories_id] => 2
        )
)

是posibble选择具有相同products_id的每个组的max tree_level。

结果应该是:

对于products_id = 13 max tree_level = 3 对于products_id = 25 max tree_level = 2

4 个答案:

答案 0 :(得分:0)

也许像dat

$tmp=0;

foreach($arr as $key=>$value){
    foreach($value as $k=>$data){
        if($k=="categories_id"){
            if($tmp<$data){
               $tmp=$data;
            }
        }
  }
}
echo $tmp; 

答案 1 :(得分:0)

因为你没有提供任何代码,我只提供了一些代码;)

填补空缺:

  马吕斯:我填补空白

$aMaxCats = array(
    array (
        "tree_level"  => 1,
        "id"  => 1,
        "products_id"  => 13,
        "categories_id"  => 1,
    ),
    array (
        "tree_level" => 2,
        "id" => 2,
        "products_id" => 13,
        "categories_id" => 2,
    ),
    array(
        "tree_level" => 3,
        "id" => 3,
        "products_id" => 13,
        "categories_id" => 3,
    ),
    array (
        "tree_level" => 1,
        "id" => 1,
        "products_id" => 25,
        "categories_id" => 1,
    ),
    array (
        "tree_level" => 2,
        "id" => 2,
        "products_id" => 25,
        "categories_id" => 2,
    )
);

 $newArray = array();

 foreach(  $aMaxCats as $product ) {
    if( !isset( $product[ $product["products_id"] ] ) ) {
        $newArray[ $product["products_id"] ] = array(
            "product_id" =>$product["products_id"],
            "categories_id"=>$product["categories_id"],
            "max_tree_level" =>$product["tree_level"]
        );
        continue;
    } else {
        if( $newArray[$product["products_id"] ]["max_tree_level"] < $product["tree_level"] ) {
            $newArray[$product["products_id"] ]["max_tree_level"] = $product["tree_level"];
        }
    }
}

// re-index array
$newArray = ___________ // google search for php reindex array

echo "<pre>";
var_dump( $newArray );

答案 2 :(得分:0)

尝试max()功能

echo(max(tree_level" => 1,"id" => 1, "products_id" => 25,"categories_id" => 1,) . "<br>");

echo (max(1,2,3));

类似

结果将是3

答案 3 :(得分:0)

您可以分组并选择最大

<?php
$aMaxCats = array(
    array (
        "tree_level"  => 1,
        "id"  => 1,
        "products_id"  => 13,
        "categories_id"  => 1,
    ),
    array (
        "tree_level" => 2,
        "id" => 2,
        "products_id" => 13,
        "categories_id" => 2,
    ),
    array(
        "tree_level" => 3,
        "id" => 3,
        "products_id" => 13,
        "categories_id" => 3,
    ),
    array (
        "tree_level" => 1,
        "id" => 1,
        "products_id" => 25,
        "categories_id" => 1,
    ),
    array (
        "tree_level" => 2,
        "id" => 2,
        "products_id" => 25,
        "categories_id" => 2,
    )
);
foreach(  $aMaxCats as $product ) {
$comapy[] = $product['products_id'];
$price[] = $product['tree_level'];
}


$group = array();
foreach($comapy as $key=>$val){
     $group[$val][] = $price[$key];
}

// this loop for check the max number and count total price
$data   = array();
$total  = 0;
foreach($group as $key=>$val){
    $data[][$key] = $key."-".current($val);
    $data[][$key] = $key."-".max($val)." Max : ".max($val);
    $total        +=max($val);
}

// this foreach to convert your data to string
$result = "";
foreach($data as $key){
    $result .= "\n".current($key);
}
// and show your data like string
print_r($result);
?>