Home  >  Article  >  Backend Development  >  Add different arrays in php

Add different arrays in php

WBOY
WBOYOriginal
2023-05-24 18:06:09467browse

In PHP, array is a very flexible and commonly used data type. It can store multiple values ​​(elements), and these values ​​can be of any data type. When we need to add different arrays, we need to understand the different addition methods and the corresponding precautions.

  1. Use the array_merge() function to merge arrays

The array_merge() function can merge two or more arrays into a new array, which will contain all input arrays. The elements are put into a new array, the index starts again from 0, and the key names of the original array can be retained without overwriting the values ​​of the same key names. The following is an example:

$array1 = array('a', 'b', 'c');
$array2 = array('d', 'e', 'f');
$array3 = array_merge($array1, $array2);
print_r($array3);

The output result is:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
)

It should be noted that if the values ​​​​of the same key name are arrays, then the array_merge() function will recursively merge them, that is Merge subarrays together. If the key name is a number, the merged result is automatically converted to a numerically indexed array.

  1. Use operators to merge arrays

In PHP, you can use operators to merge two arrays into a new array, but unlike the array_merge() function , the operator will only retain the key names and values ​​​​in the array on the left, and the values ​​​​with the same key names in the array on the right will be ignored. The following is an example:

$array1 = array('a', 'b', 'c');
$array2 = array('d' => 1, 'e', 'f');
$array3 = $array1 + $array2;
print_r($array3);

The output is:

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

It should be noted that if the key names of the array are strings, the operator will treat them as associative arrays. If the array on the left contains both numeric and string index keys, the string index keys in the right array will be ignored.

  1. Use the array_merge_recursive() function to merge arrays (recursive merging)

If the values ​​of the same key name are arrays, then using the array_merge() function to merge will only overwrite the previous one The value corresponding to the key name in an array without recursively merging sub-arrays. If you need to recursively merge subarrays with the same key name, you can use the array_merge_recursive() function. The following is an example:

$array1 = array('a' => array('b'), 'c');
$array2 = array('a' => array('d'), 'e');
$array3 = array_merge_recursive($array1, $array2);
print_r($array3);

The output result is:

Array
(
    [a] => Array
        (
            [0] => b
            [1] => d
        )

    [0] => c
    [1] => e
)

It should be noted that the array_merge_recursive() function will merge subarrays together and merge values ​​with the same key name into one array.

  1. Use the array_replace() function to merge arrays (replacement merge)

array_replace() function can merge two or more arrays into a new array, but it is different from array_merge The difference with the () function is that values ​​with the same key name will be replaced by subsequent values ​​instead of being merged into an array. The following is an example:

$array1 = array('a', 'b', 'c');
$array2 = array('b', 'd');
$array3 = array_replace($array1, $array2);
print_r($array3);

The output result is:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)

It should be noted that the array_replace() function will only replace the value of the key name. If the key name exists in an array, does not exist in the other array, then its value will remain unchanged.

In summary, PHP provides a variety of methods for merging arrays, and we can choose which method to use based on actual needs. For situations where you need to retain the original array key names, you can use the array_merge() function; if you need to recursively merge subarrays, you can use the array_merge_recursive() function; if you need to replace values ​​with the same key names instead of merging them into an array, you can use array_replace () function.

The above is the detailed content of Add different arrays in php. 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