Home > Backend Development > PHP Problem > php add to array

php add to array

WBOY
Release: 2023-05-23 10:38:36
Original
712 people have browsed it

In PHP, array is a very common data type. It can hold multiple values ​​and access and manipulate these values ​​through keys. In some cases we need to add new elements to the array. This article explains how to add new elements to an array in PHP.

1. Use the array_push() function

PHP provides the array_push() function to add one or more elements to an array. This function can accept two parameters: the array to add elements and one or more elements to add. Here is a simple example:

$fruits = array("apple", "banana", "orange");
array_push($fruits, "papaya");
print_r($fruits); // Array ( [0] => apple [1] => banana [2] => orange [3] => papaya )
Copy after login

In the above example, we defined an array $fruits containing three fruits. We then use the array_push() function to add a new element "papaya" to the end of the array, and finally use the print_r() function to output the updated array.

It should be noted that the array_push() function returns the number of elements in the new array, and if any elements are added that cause the array to have no keys, they will be allocated in ascending order. Therefore, it is recommended to pass a separate value to avoid this problem.

2. Use the " " operator

PHP's " " operator can merge two arrays into one, where the elements of the second array are appended to the end of the first array.

$fruits = array("apple", "banana", "orange");
$more_fruits = array("papaya", "mango");

$new_array = $fruits + $more_fruits;

print_r($new_array); // Array ( [0] => apple [1] => banana [2] => orange [3] => papaya [4] => mango )
Copy after login

In the above example, we defined two arrays $fruits and $more_fruits. We then merge them into a new array $new_array using the " " operator and output the result using the print_r() function.

It should be noted that if there are elements with the same key name in two arrays, the elements in the first array will be retained and the elements in the second array will be ignored.

3. Use the [] and = operators

Starting from PHP 5.4, we can use the [] and = operators to add a new element to the array. They provide a quick and simple replacement for the array_push() function.

$fruits = array("apple", "banana", "orange");
$fruits[] = "papaya";

print_r($fruits); // Array ( [0] => apple [1] => banana [2] => orange [3] => papaya )
Copy after login

In the above example, we first define an array $fruits with three elements. We then add a new element to the array by assigning the value "papaya" to a new item of it. Finally, we use the print_r() function to output the updated array.

It is important to note that when adding a new element to an array using the [] operator, if a key name is not specified, PHP will automatically assign a key using a numeric index.

Summary

This article introduces three ways to add elements to an array in PHP: using the array_push() function, using the " " operator, and using the [] and = operators. These methods are simple but may have varying applicability in different situations. Depending on the specific situation, choosing the appropriate method can help us manipulate and manage arrays more efficiently.

The above is the detailed content of php add to array. 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