How to add an element to an array in php

PHPz
Release: 2023-04-27 14:46:22
Original
545 people have browsed it

In PHP, Arrays are a very important data structure, usually used to store a set of values, and each element can be accessed through the key name. In the actual development process, we often encounter situations where we need to add or delete elements from an array. This article will mainly introduce how to add an element to an array in PHP.

In PHP, two different methods can be used to add an element, namely using the built-in function array_push() and $array[]. The specific method is as follows:

Method 1: Use the array_push() function

The array_push() function is a built-in function in PHP that allows one or more elements to be added to the end of the array. The following is the syntax of the array_push() function:

array_push(array, value1, value2, …)

Among them, array represents the array to which elements are to be added, value1, value2, etc. are the elements to be added. . The following is a sample code that uses the array_push() function to add an element:

//Create an array
$arr = array("apple", "banana", "orange ");
//Add an element to the end of the array
array_push($arr, "pear");
//Print the array
print_r($arr);
?>

In the above example code, we first create an array containing three elements. Next, the array_push() function is used to add an element "pear" to the end of the array. Finally, use the print_r() function to print out the array. The result is as follows:

Array
(

[0] => apple [1] => banana [2] => orange [3] => pear
Copy after login

)

Method 2: Use assignment statement

PHP's array can grow dynamically, that is to say, you can add elements to the end of the array through direct assignment, as shown below:

//Create Array
$arr = array("apple", "banana", "orange");
//Add an element to the end of the array
$arr[] = "pear";
//Print the array
print_r($arr);
?>

In the above sample code, we used a simpler method. Compared with using the array_push() function, we directly add [] after the array and assign an element to add the element to the end of the array.

Summary

In PHP, adding an element to an array can not only use the array_push() function, but can also be implemented directly through an assignment statement. No matter which method is used, as long as you have a good knowledge of arrays, you can easily complete operations on arrays.

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