In PHP programming, array is one of the commonly used data types. Array merging is a common operation that combines two or more arrays into a new array. During this process, sometimes it is necessary to add some additional fields to the new array to better describe the data. Therefore, this article will introduce the relevant knowledge of PHP array merging and adding fields.
1. PHP array merging
PHP provides a variety of methods to merge two or more arrays. These methods include:
Use the " " operator to merge two arrays into a new array. Its syntax is as follows:
$new_array = $array1 + $array2;
In this syntax, if there are the same key names in the array, the key values in $array1 will overwrite the key values in $array2. If elements with the same key name are both arrays, they will be merged recursively.
The array_merge() function can merge two or more arrays into a new array. Its syntax is as follows:
$new_array = array_merge($array1, $array2, ...);
In this syntax, if there are the same key names in the array, the key values in the subsequent array will overwrite the key values in the previous array. If elements with the same key name are both arrays, they will not be merged recursively, but will be treated as two separate elements.
The array_replace() function can merge two or more arrays into a new array. Its syntax is as follows:
$new_array = array_replace($array1, $array2, ...);
In this syntax, unlike the array_merge() function, the array_replace() function will recursively merge elements with the same key name in the array, instead of treating them as two independent Elements.
2. Adding fields to PHP arrays
In PHP, you can use the following method to add a new field to the array:
You can add a new field to the array by directly using the assignment statement. For example:
$person = array("name" => "Tom", "age" => 18); $person["gender"] = "Male";
In this example, we add a new field "gender" to the $person array and assign it the value "Male".
The array_push() function adds an element to the end of the array. For example:
$person = array("name" => "Tom", "age" => 18); array_push($person, "gender", "Male");
In this example, we added two new elements "gender" and "Male" to the $person array using the array_push() function.
It should be noted that when the elements in the array are associative arrays, using the array_push() function will add new elements to the end of the array and assign a numeric key name instead of using an associative key name.
The array_merge() function can not only merge arrays, but also add new elements to the array. For example:
$person = array("name" => "Tom", "age" => 18); $person = array_merge($person, array("gender" => "Male"));
In this example, we added a new element "gender" to the $person array using the array_merge() function.
The array_replace() function can not only merge arrays, but also add new elements to the array. For example:
$person = array("name" => "Tom", "age" => 18); $person = array_replace($person, array("gender" => "Male"));
In this example, we added a new element "gender" to the $person array using the array_replace() function.
It should be noted that if the same key name already exists in the array, using the array_replace() function will overwrite the original key value.
3. Sample code
Next, we will combine the sample code to demonstrate the operation of PHP array merging and adding fields.
$array1 = array("a" => "Apple", "b" => "Banana"); $array2 = array("b" => "Blueberry", "c" => "Cherry"); $new_array = $array1 + $array2; print_r($new_array);
The output result is:
Array ( [a] => Apple [b] => Banana [c] => Cherry )
$array1 = array("a" => "Apple", "b" => "Banana"); $array2 = array("b" => "Blueberry", "c" => "Cherry"); $new_array = array_merge($array1, $array2); print_r($new_array);
The output result is:
Array ( [a] => Apple [b] => Blueberry [c] => Cherry )
$array1 = array("a" => "Apple", "b" => "Banana"); $array2 = array("b" => "Blueberry", "c" => "Cherry"); $new_array = array_replace($array1, $array2); print_r($new_array);
The output result is:
Array ( [a] => Apple [b] => Blueberry [c] => Cherry )
$person = array("name" => "Tom", "age" => 18); $person["gender"] = "Male"; print_r($person);
The output result is:
Array ( [name] => Tom [age] => 18 [gender] => Male )
$person = array("name" => "Tom", "age" => 18); array_push($person, "gender", "Male"); print_r($person);
The output result is:
Array ( [name] => Tom [age] => 18 [0] => gender [1] => Male )
$person = array("name" => "Tom", "age" => 18); $person = array_merge($person, array("gender" => "Male")); print_r($person);
The output result is:
Array ( [name] => Tom [age] => 18 [gender] => Male )
$person = array("name" => "Tom", "age" => 18); $person = array_replace($person, array("gender" => "Male")); print_r($person);
The output result is:
Array ( [name] => Tom [age] => 18 [gender] => Male )
4. Summary
This article introduces the relevant knowledge of PHP array merging and adding fields, including the use of " " operator, array_merge() function, array_replace() function and direct use of assignment statements and The array_push() function adds new elements to an array. In actual development, choosing the appropriate method to operate according to needs can improve the efficiency and readability of the code.
The above is the detailed content of php array merge add fields. For more information, please follow other related articles on the PHP Chinese website!