Forum : How can I add to the beginning of an array?
Brief description  about Online courses   join in Online courses
View Abhijit  Paul 's Profile

How can I add to the beginning of an array?

hi,
Can anyone tell me how to add to the beginning of an array?
Asked by Abhijit Paul | Mar 10, 2010 |  Reply now
Replies (1)
View teacher siliconindia 's Profile
hi Abhijit,

You can do this by using array_unshift function.

<?php
$values = array(2,3,4,5,6,7,8,9,10);
$elements = array_unshift($values,1);

echo "Number of elements: $elements \n\n";
print_r($values);
?>

Here's the output:

Number of elements: 10

Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
)
Mar 10, 2010