1

以下のような配列があります。ストア 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() を使用しますが、それぞれのキーごとに最大重みが必要です。これを何時間もいじっていて、どこにも行きません!

任意のポインタをいただければ幸いです。

4

2 に答える 2

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

ここで実際に見てください:http://codepad.viper-7.com/eF4ZQ7

于 2013-01-25T15:40:32.573 に答える
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);
于 2013-01-25T15:40:42.573 に答える