Add element to array in PHP
Question:
How to add element to array in PHP? Answer:
$myArray = [1, 2, 3];
$myArray[] = 4;
// Adding multiple items at once
array_push($myArray, 5, 6, 7);
Description:
To add one element to an existing PHP array simply use the syntax: $myArray[] = $value;
However, if you want to append multiple items to the array use the built in array_push
method, that pushes one or more elements onto the end of array.
Reference:
PHP array_push reference
Share "How to add element to array in PHP?"
Related snippets:
Tags:
add element to array, append element to array, push element to array Technical term:
Add element to array in PHP