Home Backend Development PHP Problem How to delete subarray from two-dimensional array in php

How to delete subarray from two-dimensional array in php

Apr 23, 2023 pm 04:45 PM

PHP, as a widely used programming language, often needs to operate arrays, among which deleting array elements is a common operation. In PHP, a two-dimensional array refers to the value of each element in the array, which is also an array. We sometimes need to delete elements in it. In this article, we will introduce how to delete elements from a two-dimensional array in PHP.

1. Introduction to two-dimensional arrays

In PHP, an array is an ordered set of values, and each value can be assigned a unique key. When the value of an element is also an array, the array can be called a two-dimensional array. A two-dimensional array can be viewed as an array composed of several one-dimensional arrays.

The following is a simple two-dimensional array:

$students = array(
    array('name'=>'张三', 'age'=>20, 'gender'=>'男'),
    array('name'=>'李四', 'age'=>22, 'gender'=>'女'),
    array('name'=>'王五', 'age'=>24, 'gender'=>'男')
);
Copy after login

The above two-dimensional array describes the name, age and gender information of three students. Each student's information is described by a one-dimensional array, and these one-dimensional arrays form a two-dimensional array. We can access and operate elements in the two-dimensional array in the following ways:

// 访问二维数组中的元素
echo $students[0]['name']; // 输出 '张三'

// 修改二维数组中的元素
$students[1]['age'] = 23;
Copy after login

2. Delete elements in the two-dimensional array

Sometimes we need to delete an element from the two-dimensional array , PHP provides a variety of methods to achieve this operation.

1. Use the unset function

In PHP, you can use the unset function to delete specified elements from an array. For two-dimensional arrays, we can use the following method to delete elements:

unset($array[$i][$j]);
Copy after login

Among them, $i represents the subscript of the one-dimensional array where the element is to be deleted, and $j represents the two-dimensional array where the element is to be deleted. subscript. If you want to delete multiple elements, you can use multiple unset functions nested.

The following is an example that demonstrates how to use the unset function to delete elements in a two-dimensional array:

$students = array(
    array('name'=>'张三', 'age'=>20, 'gender'=>'男'),
    array('name'=>'李四', 'age'=>22, 'gender'=>'女'),
    array('name'=>'王五', 'age'=>24, 'gender'=>'男')
);

// 删除第一个学生的信息
unset($students[0]);

// 删除第二个学生的姓名信息
unset($students[1]['name']);

// 输出剩余的学生信息
print_r($students);
Copy after login

In the above code, we first use the unset function to delete the first element in the two-dimensional array $students. One element, information about the first student. Then, we used the unset function to delete the name information of the second student in $students. Finally, the print_r function is used to output the remaining student information, and you can see that the deletion was successful.

2. Use the array_splice function

In addition to using the unset function, you can also use the array_splice function to delete specified elements from a two-dimensional array.

The usage of the array_splice function is as follows:

array_splice(array &$input, int $offset [, int $length [, mixed $replacement ]]): array
Copy after login

Among them, $input represents the array to be operated on, $offset represents the subscript from which to start deleting, $length represents the number of elements to be deleted, $ replacement represents the element that needs to be added to the array. When deleting elements, the $replacement parameter can be omitted.

For two-dimensional arrays, we can use the following method to delete elements:

array_splice($array[$i], $j, 1);
Copy after login

Among them, $array represents the two-dimensional array where the element is to be deleted, and $i represents the two-dimensional array where the element is to be deleted. The subscript of the one-dimensional array, $j represents the subscript of the two-dimensional array where the element is to be deleted.

The following is an example that demonstrates how to use the array_splice function to delete elements in a two-dimensional array:

$students = array(
    array('name'=>'张三', 'age'=>20, 'gender'=>'男'),
    array('name'=>'李四', 'age'=>22, 'gender'=>'女'),
    array('name'=>'王五', 'age'=>24, 'gender'=>'男')
);

// 删除第一个学生的信息
array_splice($students, 0, 1);

// 删除第二个学生的姓名信息
array_splice($students[1], 0, 1);

// 输出剩余的学生信息
print_r($students);
Copy after login

In the above code, we first use the array_splice function to delete the first element in the two-dimensional array $students One element, information about the first student. Then, we used the array_splice function to delete the name information of the second student in $students. Finally, the print_r function is used to output the remaining student information, and you can see that the deletion was successful.

3. Summary

The two-dimensional array in PHP is an array composed of several one-dimensional arrays. Deleting elements in a two-dimensional array can be achieved using the unset function and array_splice function. Using the unset function to delete elements is simple and direct, but the memory usage of the array will not be reduced; when using the array_splice function to delete elements, you need to specify the subscript to start deleting and the number of elements to delete, which can be reduced through this function. The purpose of array memory usage.

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1264
29
C# Tutorial
1237
24