兩個二維數組的合并

來源:互聯網
上載者:User
合并前的數組a:

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

合并前的數組b:

array (size=2)  0 =>     array (size=2)      'user_id' => string '113' (length=2)      'count' => string '1' (length=1)  1 =>     array (size=2)      'user_id' => string '109' (length=2)      'count' => string '8' (length=1)

合并後的數組(合并條件,a的id==b的user_id時合并):

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=3)      'id' => string '111' (length=3)      'email' => string 'yintx_1293456456@163.com' (length=24)      'count' => string '0' (length=1)  2 =>     array (size=3)      'id' => string '109' (length=3)      'email' => string 'yintx_129@99999.com' (length=19)      'count' => string '8' (length=1)

回複內容:

合并前的數組a:

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

合并前的數組b:

array (size=2)  0 =>     array (size=2)      'user_id' => string '113' (length=2)      'count' => string '1' (length=1)  1 =>     array (size=2)      'user_id' => string '109' (length=2)      'count' => string '8' (length=1)

合并後的數組(合并條件,a的id==b的user_id時合并):

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=3)      'id' => string '111' (length=3)      'email' => string 'yintx_1293456456@163.com' (length=24)      'count' => string '0' (length=1)  2 =>     array (size=3)      'id' => string '109' (length=3)      'email' => string 'yintx_129@99999.com' (length=19)      'count' => string '8' (length=1)

array_merge_recursive() 函數與 array_merge() 函數 一樣,將一個或多個數組的元素的合并起來,一個數組中的值附加在前一個數組的後面。並返回作為結果的數組。
但是,與 array_merge() 不同的是,當有重複的鍵名時,值不會被覆蓋,而是將多個相同鍵名的值遞迴組成一個數組。(參見例子 1)

$a1=array("a"=>"Horse","b"=>"Dog");
$a2=array("c"=>"Cow","b"=>"Cat");
print_r(array_merge_recursive($a1,$a2));
?>
輸出:
Array (
[a] => Horse
[b] => Array ( [0] => Dog [1] => Cat )
[c] => Cow
)

$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)

array_merge_recursive($a, $b);

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.