我有一个类似于下面的数组。我试图获得数组中每个唯一键的最大权重值,例如商店A = 230,商店B = 180,商店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(),但需要每个相应按键的最大重量。这几个小时都搞乱了,没有到达任何地方!
任何指针都会受到赞赏。
答案 0 :(得分:0)
$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'] );
}
}
在此处查看:[{3}}
答案 1 :(得分:0)
$maxes = array();
foreach ($array as $i) {
if (!isset($maxes[$i['store']]) || $maxes[$i['store']] < $i['weight']) {
$maxes[$i['store']] = $i['weight'];
}
}
var_dump($maxes);