How do I add to the beginning of an array and find the
number of elements in it?

Answer Posted / jude jeevanraj.p

To do this, you will want to use the array_unshift function
which works like this.

<?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
)


Is This Answer Correct ?    8 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain whether it is possible to share a single instance of a memcache between multiple php projects?

712


Is session a cookie?

740


How to reset/destroy a cookie?

815


Is php a programming language?

765


What is null value in php?

749


Why is facebook still using php?

815


Explain the difference between unlink() and unset()?

732


What is a query give example?

745


What is the content of /etc directory?

769


How to connect to a url in php?

704


What are the three parts of an http request?

749


Which is better php or wordpress?

714


Explain preg_Match and preg_replace?

747


What is the difference between php and cakephp?

729


What is difference between mysql_connect and mysqli_connect?

694