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

What is the w3c?

8729


What is the use of $_server["php_self"] variable?

732


What is the use of print_r function in php?

736


What is the use of stripslashes in php?

765


Does exist in php?

756


In php, how to redirect from one page to another page?

715


Is laravel better than codeigniter?

811


How is session id stored in browser?

773


How to call a php function from html button?

813


How to select a database?

746


What is php how it works?

765


Write a program using while loop in php?

768


Explain briefly about a search-friendly site looks like?

708


What are the Formatting and Printing Strings available in PHP?

813


What is dao in php?

719