0
嗨具體數值我有數組是這樣的:如何計算在PHP數組
0 =>
array (size=6)
'id' => string '55' (length=2)
'uid' => string '1' (length=1)
'item_id' => string '5' (length=1)
'item_name' => string 'dfgdfg' (length=6)
'price' => string '14.91' (length=5)
'time' => string '1373975023' (length=10)
1 =>
array (size=6)
'id' => string '59' (length=2)
'uid' => string '1' (length=1)
'item_id' => string '5' (length=1)
'item_name' => string 'dfgdfg' (length=6)
'price' => string '14.91' (length=5)
'time' => string '1373978756' (length=10)
2 =>
array (size=6)
'id' => string '58' (length=2)
'uid' => string '1' (length=1)
'item_id' => string '6' (length=1)
'item_name' => string 'kubek #1' (length=8)
'price' => string '10.01' (length=5)
'time' => string '1373978751' (length=10)
3 =>
array (size=6)
'id' => string '56' (length=2)
'uid' => string '1' (length=1)
'item_id' => string '7' (length=1)
'item_name' => string 'ewr' (length=3)
'price' => string '1.05' (length=4)
'time' => string '1373975032' (length=10)
,我需要把這個數組是這樣的:
0 =>
array (size=6)
'id' => string '59' (length=2)
'uid' => string '1' (length=1)
'item_id' => string '5' (length=1)
'item_name' => string 'dfgdfg' (length=6)
'price' => string '14.91' (length=5)
'time' => string '1373978756' (length=10)
'q' => 2
1 =>
array (size=6)
'id' => string '58' (length=2)
'uid' => string '1' (length=1)
'item_id' => string '6' (length=1)
'item_name' => string 'kubek #1' (length=8)
'price' => string '10.01' (length=5)
'time' => string '1373978751' (length=10)
'q' => 1
2 =>
array (size=6)
'id' => string '56' (length=2)
'uid' => string '1' (length=1)
'item_id' => string '7' (length=1)
'item_name' => string 'ewr' (length=3)
'price' => string '1.05' (length=4)
'time' => string '1373975032' (length=10)
'q' => 1
需要算多少特定值進入陣列並將它們放入新陣列。這時我嘗試做這樣,但它不會。如果您使用的是數據庫的工作很好
foreach ($b as $k => $v) {
$bas[$k]['q'] = array_count_values($v['item_id']);
}
是這些值從數據庫中來?你可以使用'GROUP BY item_id'然後.. – Pieter
不錯的一個我沒有考慮它,使用count(item_id)作爲q和你說group_ item_id,非常感謝 – DorienCragen
不客氣。我會發布它是一個答案,然後其他人可以使用它。 – Pieter