這裏是我現在在做什麼東西,我從數據庫中像這樣的數組:做與A組和()的東西像MySQL查詢做,但在PHP多陣列
array (size=2)
0 =>
array (size=13)
'wop_product_owner_id' => string '100007' (length=6)
'wop_product_id' => string '100002' (length=6)
'wop_product_price' => string '18.80' (length=5)
'wop_product_count' => string '2.00' (length=4)
'wop_product_unit' => string 'kg' (length=6)
1 =>
array (size=13)
'wop_product_owner_id' => string '100007' (length=6)
'wop_product_id' => string '100001' (length=6)
'wop_product_price' => string '35.00' (length=5)
'wop_product_count' => string '4.00' (length=4)
'wop_product_unit' => string 'kg' (length=6)
所有我需要做的是「* wop_product_price wop_product_count」合併的每個項目,總爲這個陣列,它具有相同的「wop_product_owner_id」在一個新的數組,新數組應該是這樣的:
array (size=1)
0 =>
array (size=5)
'woner_id' => string '100007' (length=6)
'woner_name' => string 'Jack' (length=6)
'woner_photo' => string '' (length=5)
'woner_total' => float '177.6.00'
'woner_paid' => boolean 'false'
基本上,就像你可以在Mysql中用sum()和group進行查詢。 而且,我已經有辦法做到這一點,這裏是代碼:
$ tmps = array();
foreach ($products as $p) {
$owner_id = $p['wop_product_owner_id'];
if (empty($tmps)) {
$tmps[] = array(
'woner_id' => $p['wop_product_owner_id'],
'woner_name' => $p['wop_product_owner_name'],
'woner_photo' => $p['wop_product_owner_photo'],
'woner_total' => (float) $p['wop_product_price'] * (float) $p['wop_product_count'],
'woner_paid' => (int) $p['wop_product_payment_id'] > 0
);
} else {
foreach ($tmps as $key => $value) {
if (array_search($owner_id, $value) === FALSE) {
$tmps[] = array(
'woner_id' => $p['wop_product_owner_id'],
'woner_name' => $p['wop_product_owner_name'],
'woner_photo' => $p['wop_product_owner_photo'],
'woner_total' => (float) $p['wop_product_price'] * (float) $p['wop_product_count'],
'woner_paid' => (int) $p['wop_product_payment_id'] > 0
);
} else {
$tmps[$key]['woner_total'] = (float) $tmps[$key]['woner_total'] + (float) $p['wop_product_price'] * (float) $p['wop_product_count'];
}
}
}
}
return $tmps;
但是,我實際上並不喜歡它,我以爲肯定會有比這更優美的方式!你怎麼看?有任何想法嗎?
感謝你的帖子,你的方式看起來比我好! – Jack