Home >Backend Development >PHP Problem >How to add elements to an array in php
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.
#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 methodRecommended 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!