Home>Article>Backend Development> How to delete the first row of elements from a two-dimensional array in php

How to delete the first row of elements from a two-dimensional array in php

青灯夜游
青灯夜游 Original
2022-07-28 16:42:39 2165browse

Two methods to delete the first row of elements: 1. Use the array_shift() function to delete, which can delete the beginning element of a two-dimensional array (whether the element is a single value or an array), the syntax is " array_shift(two-dimensional array)". 2. Use the array_splice() function to delete. Just set the second parameter of the function to 0 and the third parameter to 1. The syntax is "array_splice (two-dimensional array, 0,1)".

How to delete the first row of elements from a two-dimensional array in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

php two-dimensional array deletion removes the first row of elements , which is to delete the first element of the two-dimensional array (whether the element is a single value or an array).

You can use two function implementation methods:

  • Use the array_shift() function

  • Use array_splice() function

Method 1: Use array_shift() function to delete

PHP array_shift() function is used to delete elements at the beginning of the array, The syntax is as follows:

array_shift(array)
  • The parameter arr represents the array to be processed.

array_shift() function will remove the first element at the beginning of the arr array and return it as the result. The length of the arr array is decremented by 1 and all other elements are moved forward by one. All numeric key names will be changed to start counting from 0, and string key names will remain unchanged.

Return value: Returns the value of the element removed from the array, or NULL if the array is empty.

Example 1: If the first row of the two-dimensional array is an element

How to delete the first row of elements from a two-dimensional array in php

Example 2: If the first row of the two-dimensional array is an array (One row of values)

How to delete the first row of elements from a two-dimensional array in php

Method 2: Use array_splice() function to delete

PHP array_splice() function is used to delete arrays part of the elements; you can delete them directly or replace them with other values. The deletion syntax is as follows:

array_splice(array,start,length)
Parameter Description
array Required. Specifies an array.
start Required. numerical value. Specifies the starting position of deleted elements. 0 = first element. If the value is set to a positive number, removal begins at the offset specified by the value in the array. If the value is set to a negative number, removal begins at the offset specified by the value from the end of the array. -2 means start from the second to last element of the array.
length Optional. numerical value. Specifies the number of elements to be removed, which is also the length of the returned array. If this value is set to a positive number, remove this number of elements. If this value is set to a negative number, all elements from start to length inverse of the end of the array are removed. If this value is not set, all elements from the position set by the start parameter to the end of the array are removed.

Return value: Returns the array containing the extracted elements.

Just set the second parameter of the function to 0 and the third parameter to 1 to delete the first row of elements

Example:

How to delete the first row of elements from a two-dimensional array in phpHow to delete the first row of elements from a two-dimensional array in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to delete the first row of elements from a two-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn