夥計我想總結兩個數組字段liked_cnt , coupon_cnt
如果第一個字段id
是相同的 我該怎麼做?如何總結2特定字段的數組,如果id相同在PHP
我這樣做,但它的toooooo緩慢
if (end($like)['id'] > end($coupon)['id']) {
$i = end($like)['id'];
$j = end($coupon)['id'];
}else {
$i = end($coupon)['id'];
$j = end($like)['id'];
}
for ($k=0; $k <= $i; $k++) {
for ($l=0; $l < $j; $l++) {
if ($like['id'] == $coupon['id']) {
$score[$like['id']] = ($coupon['coupon_cnt'] * 1000) + $like['liked_cnt'];
}else {
$score[$i] = 0;
}
}
}
//first array $Like
Array ([0] => Array ([id] => 85 [liked_cnt] => 6) [1] => Array ([id] => 86 [liked_cnt] => 14) [2] => Array ([id] => 92 [liked_cnt] => 6) [3] => Array ([id] => 93 [liked_cnt] => 6)
//second array $coupon
Array ([0] => Array ([id] => 35 [coupon_cnt] => 2) [1] => Array ([id] => 86 [coupon_cnt] => 1) [2] => Array ([id] => 139 [coupon_cnt] => 1))
你有什麼試過,發佈了你的代碼 – Rahul
@Rahul我更新了代碼,請幫忙 –