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 ) )
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).
shift() The shift() method removes the first element from an array and returns that removed element. This method changes the length of the 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.
The array_shift() function removes the first element from an array, and returns the value of the removed element.
You can use
array_shift($array)
Documentation for array_shift
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 )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With