How can I merge PHP arrays?

前端 未结 10 1793
盖世英雄少女心
盖世英雄少女心 2020-12-20 12:43

I have two arrays of animals (for example).

$array = array(
    array(
        \'id\' => 1,
        \'name\' => \'Cat\',
    ),
    array(
        \'id         


        
10条回答
  •  囚心锁ツ
    2020-12-20 13:27

    foreach ($array as $a)
        $new_array[$a['id']]['name'] = $a['name'];
    
    foreach ($array2 as $a)
        $new_array[$a['id']]['age'] = $a['age'];
    

    and this is result:

        [1] => Array
            (
                [name] => Cat
                [age] => 123
            )
    
        [2] => Array
            (
                [name] => Mouse
                [age] => 321
            )
    

提交回复
热议问题