How to append elements to php array

PHPz
Release: 2023-04-24 16:00:56
Original
1176 people have browsed it

Arrays are a common data type. In PHP, arrays can be created and modified in many ways. Array appending is a common operation, typically used to add new data to the end of an existing array. In this article, we will introduce the basic methods and examples of array append in PHP.

  1. Use PHP built-in function array_push()

PHP’s array_push() function is a simple way to add one or more elements to the end of an array. The syntax of this function is as follows:

array_push(array, value1, value2, …)

Among them, array is the array to which elements are to be appended, and value1, value2, etc. are the elements to be appended to the end of the array. . One or more elements can be added to an array at the same time. For example, the following code demonstrates how to add new elements to an array using the array_push() function:

$fruits = array("apple", "banana", "orange");
array_push($fruits, "grape", "melon"); // 向数组中添加2个元素
print_r($fruits); // 输出:Array([0] => apple [1] => banana [2] => orange [3] => grape [4] => melon)
Copy after login

In the above example, we have created an array containing 3 fruit names. We then added two new elements, grapes and melon, to the array using the array_push() function. Finally, we use the print_r() function to display the modified array. As you can see, the new element has been successfully added to the end of the array.

  1. Use the assignment operator =

Another way to append to an array is to use the assignment operator =. This operator appends one or more elements to an array. Like this:

$array1 = array("apple", "banana");
$array2 = array("orange", "grape");
$array1 += $array2;
print_r($array1); // 输出:Array([0] => apple [1] => banana [2] => orange [3] => grape)
Copy after login

In the above example, we created two arrays and appended $array2 array to $array1 array using = operator. Finally, we use the print_r() function to display the modified $array1 array.

Please note that the = operator will only add an element if the key name does not exist, if the key name already exists, the new value will be ignored.

  1. Use the bracket notation []

You can also use the bracket notation [] to add new elements directly to the end of the array. The syntax is as follows:

$array[] = $value;
Copy after login

Among them, $array is the array to which elements are to be appended, and $value is the element to be appended to the end of the array. For example, the following code demonstrates how to add a new element to an array:

$fruits[] = "apple";
$fruits[] = "banana";
$fruits[] = "orange";
print_r($fruits); // 输出:Array([0] => apple [1] => banana [2] => orange)
Copy after login

In the above example, we first created an array named $fruits using an empty array. As you can see, the $fruits array is currently empty. Then we added three new elements to the array using bracket notation []. Finally, we use the print_r() function to display the modified array.

Summary

In PHP, appending new elements to the end of an array is a common operation. This article introduces three basic methods to implement array appending: using the PHP built-in function array_push(), using the assignment operator =, and using the bracket symbol []. The specific implementation method can be selected according to the actual situation.

The above is the detailed content of How to append elements to php 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!