How can I merge PHP arrays?

前端 未结 10 1825
盖世英雄少女心
盖世英雄少女心 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:31

    With PHP 5.3 you can do this sort of merge with array_replace_recursive()

    http://www.php.net/manual/en/function.array-replace-recursive.php

    You're resultant array should look like:

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

    Which is what I think you wanted as a result.

提交回复
热议问题