This article introduces how to delete element values from the head of a PHP array. Friends in need can refer to it.
This section introduces how to delete values from the php array header, This can be achieved with the php array function array_shift(), which removes and returns the element found in the array. The result is that if numeric keys are used, all corresponding values are shifted down, while arrays using associated keys are unaffected. The form is: mixed array_shift(array array) Example: Delete the first element apple in the $fruits array: <?php $fruits = array("apple","banana","orange","pear"); $fruit = array_shift($fruits); // $fruits = array("banana","orange","pear") // $fruit = "apple"; ?> Copy after login Introduced the method of deleting values from the array header. Here are the methods of deleting elements in the array for your reference. Delete elements from php array If you want to delete an element from an array, you can use unset directly. For example: <?php $arr = array('a','b','c','d'); unset($arr[1]); print_r($arr); ?> Copy after login But something strange happened. After print_r($arr), the result was not like that. The final result was Array ( [0] => a [2] => c [3] => d ) How can the missing elements be filled and the array re-indexed? At this time, the array_splice() function needs to appear: <?php $arr = array('a','b','c','d'); array_splice($arr,1,1); print_r($arr); ?> Copy after login In this way, after print_r($arr), the result is Array ([0] => a [1] => c [2] => d). Let’s introduce these, I hope it will help you master the method of deleting the specified element value in the php array. |