首页 > 代码库 > PHP合并 两个二维数组

PHP合并 两个二维数组

$a = array(
    array(‘id‘=>‘113‘,‘email‘=>‘yintx_1292342352@163.com‘), 
    array(‘id‘=>‘111‘,‘email‘=>‘yintx_1293456456@163.com‘), 
    array(‘id‘=>‘109‘,‘email‘=>‘yintx_129@99999.com‘)
    );

$b = array(
    array(‘user_id‘=>‘113‘,‘count‘=>‘1‘), 
    array(‘user_id‘=>‘109‘,‘count‘=>‘8‘)
    );

foreach($a as $ka=>$va){
    foreach($b as $kb=>$vb){
        if($va[‘id‘] == $vb[‘user_id‘]){
            $a[$ka][‘count‘] = $vb[‘count‘];
        }
    }
}

var_dump($a);

----------

array (size=3)
  0 => 
    array (size=3)
      ‘id‘ => string ‘113‘ (length=3)
      ‘email‘ => string ‘yintx_1292342352@163.com‘ (length=24)
      ‘count‘ => string ‘1‘ (length=1)
  1 => 
    array (size=2)
      ‘id‘ => string ‘111‘ (length=3)
      ‘email‘ => string ‘yintx_1293456456@163.com‘ (length=24)
  2 => 
    array (size=3)
      ‘id‘ => string ‘109‘ (length=3)
      ‘email‘ => string ‘yintx_129@99999.com‘ (length=19)
      ‘count‘ => string ‘8‘ (length=1)

 

PHP合并 两个二维数组