2017-06-25 54 views
-1

夥計我想總結兩個數組字段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)) 
+0

你有什麼試過,發佈了你的代碼 – Rahul

+0

@Rahul我更新了代碼,請幫忙 –

回答

1

沒有神奇的算法內置到PHP能夠真正計算以更有效的方式你想要的結果。這可能會更有效,但最重要的是它更易讀:

<?php 
$input = [ 
    [ 
     ['id' => 85, 'liked_cnt' => 6], 
     ['id' => 86, 'liked_cnt' => 14], 
     ['id' => 92, 'liked_cnt' => 6], 
     ['id' => 93, 'liked_cnt' => 6] 
    ], 
    [ 
     ['id' => 35, 'coupon_cnt' => 2], 
     ['id' => 86, 'coupon_cnt' => 1], 
     ['id' => 139, 'coupon_cnt' => 1] 
    ] 
]; 

$output = []; 
foreach ($input as $set) { 
    array_walk($set, function($entry) use (&$output) { 
     $count = array_pop($entry); 
     $id = array_pop($entry); 
     if (array_key_exists($id, $output)) { 
      $output[$id]['total_cnt'] += $count; 
     } else { 
      $output[$id] = ['id' => $id, 'total_cnt' => $count]; 
     } 
    }); 
} 
print_r(array_values($output)); 

輸出顯然是:

Array 
(
    [0] => Array 
     (
      [id] => 85 
      [total_cnt] => 6 
     ) 

    [1] => Array 
     (
      [id] => 86 
      [total_cnt] => 15 
     ) 

    [2] => Array 
     (
      [id] => 92 
      [total_cnt] => 6 
     ) 

    [3] => Array 
     (
      [id] => 93 
      [total_cnt] => 6 
     ) 

    [4] => Array 
     (
      [id] => 35 
      [total_cnt] => 2 
     ) 

    [5] => Array 
     (
      [id] => 139 
      [total_cnt] => 1 
     ) 

) 

UPDATE:

考慮下面我你的第三點意見添加此修改後的版本,引入coupon_cnt屬性的一般乘法因子1000:

<?php 
$input = [ 
    [ 
     ['id' => 85, 'liked_cnt' => 6], 
     ['id' => 86, 'liked_cnt' => 14], 
     ['id' => 92, 'liked_cnt' => 6], 
     ['id' => 93, 'liked_cnt' => 6] 
    ], 
    [ 
     ['id' => 35, 'coupon_cnt' => 2], 
     ['id' => 86, 'coupon_cnt' => 1], 
     ['id' => 139, 'coupon_cnt' => 1], 
     ['id' => 99, 'coupon_cnt' => 99] 
    ] 
]; 

$output = []; 
foreach ($input as $set) { 
    array_walk($set, function($entry) use (&$output) { 
     $count = array_key_exists('coupon_cnt', $entry) 
       ? 1000 * array_pop($entry) 
       : array_pop($entry); 
     $id = array_pop($entry); 
     if (array_key_exists($id, $output)) { 
      $output[$id]['total_cnt'] += $count; 
     } else { 
      $output[$id] = ['id' => $id, 'total_cnt' => $count]; 
     } 
    }); 
} 
print_r(array_values($output)); 
+0

謝謝你的回答是對的,而且有用,但是你能否幫我多一點來乘以'(coupon_cnt * 1000)+ liked_cnt'謝謝你 –

+1

@MasoudTavakkoli你的意思是'$ output [$ id] ['total_cnt'] + = 1000 * $ count;'? – arkascha

+0

感謝您的幫助,這是工作 –

相關問題