Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP remove the first index of an array and re-index

Tags:

I have an array like Array

(     [0] => A     [2] => B     [4] => C     [6] => D ) 

I want to remove the first element and then re-index array to get the output

(     [0] => B     [1] => C     [2] => D ) 

Which PHP function i need to use?


Update

Input array is

Array (     [0] => Array         (             [0] => Some Unwanted text             [1] => You crazyy         )      [2] => Array         (             [0] => My belowed text             [1] => You crazyy         )      [10] => Array         (             [0] => My loved quote             [1] => You crazyy         )  ) 

And the output should be like

Array (     [0] => Array         (             [0] => My belowed text             [1] => You crazyy         )      [1] => Array         (             [0] => My loved quote             [1] => You crazyy         )  ) 
like image 398
Mithun Sreedharan Avatar asked Jun 09 '10 05:06

Mithun Sreedharan


People also ask

What does Array_splice () function do?

The array_splice() function removes selected elements from an array and replaces it with new elements. The function also returns an array with the removed elements. Tip: If the function does not remove any elements (length=0), the replaced array will be inserted from the position of the start parameter (See Example 2).

How do you remove the first index of an array?

shift() The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.

How do you return the index of an element in an array?

To find the position of an element in an array, you use the indexOf() method. This method returns the index of the first occurrence the element that you want to find, or -1 if the element is not found.

How do you delete the first element of an array in PHP?

The array_shift() function removes the first element from an array, and returns the value of the removed element.


2 Answers

You can use

array_shift($array) 

Documentation for array_shift

like image 162
User123 Avatar answered Oct 05 '22 13:10

User123


With array_splice.

http://www.php.net/manual/en/function.array-splice.php

 php > print_r($input); Array (     [0] => A     [2] => B     [4] => C     [6] => D ) php > array_splice($input, 0, 1); php > print_r($input); Array (     [0] => B     [1] => C     [2] => D )  
like image 24
Epeli Avatar answered Oct 05 '22 15:10

Epeli