To add an element to the beginning of an array in PHP, you can use the array_unshift()
function. Here's an example:
$myArray = [2, 3, 4, 5];
$newElement = 1;
array_unshift($myArray, $newElement);
print_r($myArray);
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
In this example, the array_unshift()
function adds the $newElement
(1) to the beginning of the $myArray
array. The resulting array is then printed using the print_r()
function.