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

青灯夜游
Release: 2023-03-16 16:30:01
Original
2252 people have browsed it

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)
Copy after login
  • 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

<?php  
header("content-type:text/html;charset=utf-8");
$arr = array(1,2,3,array(4,5,6)); 
echo "原二维数组:";
var_dump($arr);
array_shift($arr);
echo "删除第一行元素:";
var_dump($arr);
?>
Copy after login

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)

<?php  
header("content-type:text/html;charset=utf-8");
$arr = array(array(1,2),3,array(4,5,6),7,8,); 
echo "原二维数组:";
var_dump($arr);
array_shift($arr);
echo "删除第一行元素:";
var_dump($arr);
?>
Copy after login

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)
Copy after login
ParameterDescription
array Required. Specifies an array.
startRequired. 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:

<?php  
header("content-type:text/html;charset=utf-8");
$arr1 = array(1,2,3,array(4,5,6)); 
echo "原二维数组:";
var_dump($arr1);
array_splice($arr1, 0,1);
echo "删除第一行元素:";
var_dump($arr1);

$arr2 = array(array(1,2),3,array(4,5,6),7,8,); 
echo "原二维数组:";
var_dump($arr2);
array_shift($arr2);
echo "删除第一行元素:";
var_dump($arr2);
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template