Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取多维数组中每个唯一键的最大值_Php_Multidimensional Array - Fatal编程技术网

Php 获取多维数组中每个唯一键的最大值

Php 获取多维数组中每个唯一键的最大值,php,multidimensional-array,Php,Multidimensional Array,我有一个类似于下面的数组。我正在尝试获取数组中每个唯一键的最大权重值,例如Store A=230、Store B=180、Store C=439,品牌键也是如此 Array ( [0] => Array ( [cid] => 123 [weight] => 230 [store] => Store A [brand] => Brand A) [1] => Array ( [cid] => 124 [weight] => 180 [store] =

我有一个类似于下面的数组。我正在尝试获取数组中每个唯一键的最大权重值,例如Store A=230、Store B=180、Store C=439,品牌键也是如此

Array ( 
[0] => Array ( [cid] => 123 [weight] => 230 [store] => Store A [brand] => Brand A)
[1] => Array ( [cid] => 124 [weight] => 180 [store] => Store B [brand] => Brand B ) 
[2] => Array ( [cid] => 131 [weight] => 439 [store] => Store C [brand] => Brand B ) 
[3] => Array ( [cid] => 128 [weight] => 124 [store] => Store B [brand] => Brand B ) 
[4] => Array ( [cid] => 130 [weight] => 249 [store] => Store C [brand] => Brand C ) 
)
我可以获得整个数组的最大权重值(使用max(),但需要每个键的最大权重。我已经处理了几个小时了,什么都没有得到

任何指点都将不胜感激

$array = $array() // Your array
$max = array();

foreach ( $array as $product) {
    if ( ! isset( $max[ $product['store'] ] ) ) {
        $max[ $product['store'] ] = $product['weight'];
    } else {
        $max[ $product['store'] ] = max( $max[ $product['store'] ], $product['weight'] );
    }
}
请参见此处的操作:

$maxes=array();
foreach($i数组){
如果(!isset($maxes[$i['store']])| |$maxes[$i['store']]]<$i['weight']){
$maxes[$i['store']=$i['weight'];
}
}
变量转储(最大值);

这看起来像是类似的问题:非常感谢。如何返回相关的“cid”值,而不是输出数组中的权重值?将最后一个“权重”更改为“cid”会产生一个奇怪的结果。即,生成的cid不是最重的id。谢谢。在maxes数组中存储的不仅仅是权重。我将留给您很好,谢谢。deceze的解决方案稍微干净一点,但两种解决方案都很有效。
$maxes = array();

foreach ($array as $i) {
    if (!isset($maxes[$i['store']]) || $maxes[$i['store']] < $i['weight']) {
        $maxes[$i['store']] = $i['weight'];
    }
}

var_dump($maxes);