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



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

Answer / 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

More PHP Interview Questions

1. Create student database. 2. First page should display the students available in the database. There should be add, edit and delete buttons. 3. There should be option to search students by name, code, date of joining, department or combination of these. 4. Should have an add/edit screen. Add and Edit should be handled in the same page. 5. Delete should ask for confirmation before deleting the actual record. 6. Validation should be done in JavaScript as well as php.

0 Answers   Zonex,


How to make database connection in php?

0 Answers  


Which array function checks if the particular key exists in the array?

0 Answers  


Does apache use php?

0 Answers  


Which method do you follow to get a record from a million records? (Searching not from database, from an array in php)?

0 Answers  


Why do we use htaccess and where?

0 Answers  


Suppose your Zend engine supports the mode <? ?> Then how can u configure your PHP Zend engine to support <?PHP ?> mode ?

1 Answers   Rushmore Consultancy,


I am trying to assign a variable the value of 0123, but it keeps coming up with a different number, what is the problem?

0 Answers  


What is the meaning of php?

0 Answers  


What is the importance of php?

0 Answers  


Tips to optimize the php script..... Suggestion for exception handling in php...

1 Answers  


I have 10 elements of Array, how can i remove 5 array element, with single function.

13 Answers  


Categories