php array merge add fields

王林
Release: 2023-05-07 17:34:10
Original
547 people have browsed it

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:

  1. Use the " " operator

Use the " " operator to merge two arrays into a new array. Its syntax is as follows:

$new_array = $array1 + $array2;
Copy after login

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.

  1. Use the array_merge() function

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

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.

  1. Use the array_replace() function

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

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:

  1. Use the assignment statement directly

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

In this example, we add a new field "gender" to the $person array and assign it the value "Male".

  1. Using the array_push() function

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

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.

  1. Using the array_merge() function

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

In this example, we added a new element "gender" to the $person array using the array_merge() function.

  1. Using the array_replace() 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"));
Copy after login

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.

  1. Use the " " operator
$array1 = array("a" => "Apple", "b" => "Banana");
$array2 = array("b" => "Blueberry", "c" => "Cherry");
$new_array = $array1 + $array2;
print_r($new_array);
Copy after login

The output result is:

Array
(
    [a] => Apple
    [b] => Banana
    [c] => Cherry
)
Copy after login
  1. Use the array_merge() function
$array1 = array("a" => "Apple", "b" => "Banana");
$array2 = array("b" => "Blueberry", "c" => "Cherry");
$new_array = array_merge($array1, $array2);
print_r($new_array);
Copy after login

The output result is:

Array
(
    [a] => Apple
    [b] => Blueberry
    [c] => Cherry
)
Copy after login
Copy after login
  1. Use the array_replace() function
$array1 = array("a" => "Apple", "b" => "Banana");
$array2 = array("b" => "Blueberry", "c" => "Cherry");
$new_array = array_replace($array1, $array2);
print_r($new_array);
Copy after login

The output result is:

Array
(
    [a] => Apple
    [b] => Blueberry
    [c] => Cherry
)
Copy after login
Copy after login
  1. Add to the array New field
$person = array("name" => "Tom", "age" => 18);
$person["gender"] = "Male";
print_r($person);
Copy after login

The output result is:

Array
(
    [name] => Tom
    [age] => 18
    [gender] => Male
)
Copy after login
Copy after login
Copy after login
$person = array("name" => "Tom", "age" => 18);
array_push($person, "gender", "Male");
print_r($person);
Copy after login

The output result is:

Array
(
    [name] => Tom
    [age] => 18
    [0] => gender
    [1] => Male
)
Copy after login
$person = array("name" => "Tom", "age" => 18);
$person = array_merge($person, array("gender" => "Male"));
print_r($person);
Copy after login

The output result is:

Array
(
    [name] => Tom
    [age] => 18
    [gender] => Male
)
Copy after login
Copy after login
Copy after login
$person = array("name" => "Tom", "age" => 18);
$person = array_replace($person, array("gender" => "Male"));
print_r($person);
Copy after login

The output result is:

Array
(
    [name] => Tom
    [age] => 18
    [gender] => Male
)
Copy after login
Copy after login
Copy after login

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!

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