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

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:

$arr = array('item2', 'item3', 'item4');
array_unshift($arr , 'item1');
print_r($arr);

Output:

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

 

About Mohammad Fareed

Software Engineer @ Tekzenit.

Check Also

Scientist Finds Breakthrough Weight Loss Formula!

Don’t act so surprised, Your Highness. You weren’t on any mercy mission this time. Several …

Leave a Reply

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