PHP Questions Home
Categories
Arrays
Files
Forms
Creating PDFs
PHP Errors
Functions
Images
MySQL
Numbers
Others
Strings
Website

PHP Forums
Forums Index
MySQL Forum
PHP Forum
Flash/Actionscript
Javascript
Web design

More PHP
PHP Functions
Top Questions
Ask a Question
PHP Interview Questions
PHP Jobs
Ecommerce
IT Questions
Business Websites

Sponsors

sponsor ad
My Salary
PHP Developer Salary
Advertise Here

How do I remove the first element from an array?


Often with an array the key of the first element in the array won't simply be '0'.

Often you want to add or remove values from the start or end of an array. To remove the first value in the array and see what it is, you can use the array_shift function in PHP:

<?php
$values 
= array(1,2,3,4,5,6,7,8,9,10);
$first array_shift($values);
echo 
"First value was: $first";
print_r($values);
?>


Which tells us this:

First value was: 1

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

Notice how the keys get re-assigned after the call to array_shift, so that '2' now has a key of zero whereas in the initial array that key referenced the number '1' which has now been removed from the array.



ASK A QUESTION

More arrays PHP Questions

How do I find if a value is already in an array or not?

How do I return all the values in an array?

im getting a Resource id #5 $rs_comp = mysql_query("select * from company where autoID='$row_settings[compid]'");

What is the starting salary of Php developer in india?? after 1 year experience how much i could get??

HOW CAN I DELETE ALL THE ELEMENT OF THE ARRAY



Custom Search