SoFunction
Updated on 2025-03-10

Adding php array array("a")+array("b") or array("a")

I saw a question online:

The result of array("a")+array("b") is ___

("a","b")
("b","a")
("b")
("a")

Answer: D

Why does the result remain unchanged when two arrays of php are added?

Because, they are equivalent to array("0″=>"a")+array("0″=>"b"), their key names are the same, the former cannot be overwritten by the latter, if it is array("0″=>"a")+array("0″=>"b", "1″=>"c"), then the result is equal to array("0″=>"a","1″=>"c")

What happens if it is in the same array and there are the same key names?

See a piece of code in the official php manual:

Copy the codeThe code is as follows:
$switching = array(         10, // key = 0
                    5    =>  6,
                    3    =>  7, 
                    'a'  =>  4,
                            11, // key = 6 (maximum of integer-indices was 5)
                    '8'  =>  2, // key = 8 (integer!)
                    '02' => 77, // key = '02'
                    0    => 12  // the value 10 will be overwritten by 12
                  );

It can be seen that if there is the same key name in the same array, the value of the previous key name will be overwritten.