Home >Backend Development >PHP Problem >How to add value to php array
How to add value to php array: First create a PHP sample file; then create an array through "array("red","green");"; finally through "array_push($a,"blue" ,"yellow");" method to add new element values.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
array_push() function to the array of the first parameter Appends one or more elements to the end (pushes them onto the stack) and returns the length of the new array.
This function is equivalent to calling $array[] = $value multiple times.
Tips and Notes
Note: Even if there are string key names in the array, the elements you add are always numeric keys. (See Example 2)
Note: If you use array_push() to add a unit to the array, it is better to use $array[] =, because there is no additional burden of calling the function.
Note: array_push() will issue a warning if the first argument is not an array. This is different from the behavior of $var[], which creates a new array.
Syntax
array_push(array,value1,value2...)
Parameters
array required. Specifies an array.
value1 Required. Specifies the value to add.
value2 Optional. Specifies the value to add.
Code example:
Insert "blue" and "yellow" to the end of the array:
<?php $a=array("red","green"); array_push($a,"blue","yellow"); print_r($a); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to add value to php array. For more information, please follow other related articles on the PHP Chinese website!