Home  >  Article  >  Backend Development  >  How to append elements to an array in php (two methods)

How to append elements to an array in php (two methods)

PHPz
PHPzOriginal
2023-04-18 10:24:155498browse

PHP is a widely used server-side scripting language. In PHP, an array is a commonly used data type that is used to store multiple values. In actual development, it is often necessary to append one or more elements to the end of an array. This article will introduce how to append elements to a PHP array.

In PHP, there are two ways to append elements to an array: using the array_push() function and directly accessing the array.

Method 1: Use the array_push() function

The array_push() function is a shortcut provided by PHP to append elements to the end of the array. Its syntax is as follows:

array_push ( array &$array , mixed $value1 [, mixed $... ] ) : int

Among them, $array represents the array to be operated on; $value1 Represents the first element to be added to the end of the array, and multiple elements can be added later.

Here is a sample program that creates an array of three elements and then uses the array_push function to append two elements to the array:

$arr = array('apple', 'banana', 'orange');
array_push($arr, 'grape', 'watermelon');
print_r($arr);

?>

The output result is as follows:

Array
(

[0] => apple
[1] => banana
[2] => orange
[3] => grape
[4] => watermelon

)

As can be seen from the output result, the array_push() function Ability to add one or more elements to the end of an array.

Method 2: Directly access the array

In addition to using the array_push() function, you can also directly access the array to add new elements to its end. In fact, the PHP array itself has an array length attribute count(), through which the length of the array can be obtained. So we just need to use this property to determine where the end of the array is and then add a new element to that position.

The following is a sample program that creates an array of three elements and then appends two elements to the array using direct access to the array:

$arr = array('apple', 'banana', 'orange');
$arr[count($arr)] = 'grape';
$arr[count($arr)] = 'watermelon';
print_r($arr);

?>

The output result is as follows:

Array
(

[0] => apple
[1] => banana
[2] => orange
[3] => grape
[4] => watermelon

)

It can be seen from the output result that through By accessing the array directly, you can also add elements to the end of the array.

Summary

This article introduces two methods of appending elements to a PHP array, namely using the array_push() function and directly accessing the array. No matter which method you use, you can easily add new elements to PHP arrays. In actual development, you can choose which method to use based on specific needs.

The above is the detailed content of How to append elements to an array in php (two methods). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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