php array addition

藏色散人
Release: 2023-02-24 07:30:02
Original
4871 people have browsed it

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'];
Copy after login

B array

$b = ['c', 'd', 'e'];
Copy after login

A B result

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

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!