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

How to redirect a url from http to https in .htaccess?

523


What is the difference between super () and this ()?

555


How to check a key exist in an array?

580


What is the super method?

568


What can php do?

551






How we get ip address of client, previous reference page etc?

564


What function should you use to join array elements with a glue string?

554


What is difference between php and wordpress?

506


Do you know what is the difference between mysql_fetch_object() and mysql_fetch_array()?

526


How to define a function with any number of arguments?

570


Explain what is meant by pear in php?

535


Tell me what is the difference between characters 34 and x34?

525


How can cross site request forgery csrf be prevented?

489


What is a stored procedure in mysql?

534


What is asort php?

538