Adding Elements to an Array
Using [ ]
Syntax:
You can add elements to an array by assigning values to new or existing keys using square brackets [ ]
.
$array = []; // Initialize an empty array
$array[] = "element1"; // Add "element1" to the end of the array
$array[] = "element2"; // Add "element2" to the end of the array
Using array_push( )
Function:
PHP array_push()
appends one or more elements to the end of an array.
$array = [ ]; // Initialize an empty array
array_push($array, "element1", "element2");
// Add "element1" and "element2" to the end of the array
How to add and remove Elements from Array in PHP?
Adding and removing elements from arrays in PHP can be achieved using various built-in functions and array manipulation techniques.
Contact Us