How can you insert value/array to the beginning of an array in PHP?

Use array_unshift() to insert the first element in an array.
Use array_shift() to removes the first element of an array.

array_unshift($array, $item);

Example:

[php]
$arr = array(‘item2’, ‘item3’, ‘item4’);
array_unshift($arr , ‘item1’);
print_r($arr);
[/php]

Output:

[php]
Array
(
[0] => item1
[1] => item2
[2] => item3
[3] => item4
)
[/php]

 

Add a Comment

Your email address will not be published. Required fields are marked *