Home >Backend Development >PHP Problem >How to add elements to an array in php

How to add elements to an array in php

藏色散人
藏色散人Original
2021-11-24 09:46:174145browse

php method to add elements to an array: 1. Add one or more elements to the end of the array of the first parameter through the "array_push" function; 2. Add one or more elements to the array through the "$arr[]" method Add elements.

How to add elements to an array in php

#The operating environment of this article: Windows7 system, PHP7.1, Dell G3 computer.

php How to add elements to an array?

PHP adds elements to an array

There are two ways for PHP to add elements to an array:

array_push() , $arr[]

The first one:

$arr = array();
array_push($arr, e1, e2 ... en);
## The #array_push() function adds (push) one or more elements to the end of the first argument's array and returns the length of the new array.

This function is equivalent to calling $array[] = $value multiple times.

Second method:

$arr = array();
$arr[] = e1;
$arr[] = e2;
...
$arr[] = en;

The second method will be much faster than the first method

Recommended learning: "

PHP Video Tutorial"

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