How to add elements to an array in php

藏色散人
Release: 2023-03-14 06:24:01
Original
4117 people have browsed it

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[]
Copy after login

The first one:

$arr = array();
array_push($arr, e1, e2 ... en);
Copy after login
## 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;
Copy after login
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!

Related labels:
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