Home  >  Article  >  Backend Development  >  php array addition

php array addition

藏色散人
藏色散人Original
2019-09-16 13:09:024901browse

php array addition

How to add arrays in php

The array function in php is very powerful, and you can even add it directly to merge arrays.

A array

$a = ['a', 'b'];

B array

$b = ['c', 'd', 'e'];

A B result

Array
(
    [0] => a
    [1] => b
    [2] => e
)

You can see that for the same key value, the previous one will overwrite the later one.

This is different from the commonly used function array_merge:

array_merge for string key values, the latter array will overwrite the previous one

array_merge for numeric keys In terms of value, the following array will be merged with the previous one and re-indexed

Complete code

 'jack', 'age' => 20];
$b = ['name' => 'tom', 'age' => 21, 'gender' => 'male'];
print_r($a + $b);

Related recommendations: "PHP Tutorial"

The above is the detailed content of php array addition. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn