Home > Hosting > Program

How to delete elements in php two-dimensional array

2024-03-28 14:22:25

In PHP, we often use arrays to store a set of data, and two-dimensional arrays in PHP are often used. When there are many elements in a two-dimensional array and some elements need to be deleted, we can use some built-in functions of PHP to realize it.

First, use the unset function

Unset function in PHP can set the value of a variable to null, or delete the elements in the array. If the specified element is deleted, you can use the parameters of the unset function to specify the location of the element. The following is an example of deleting a specified element in a two-dimensional array:

//Define a two-dimensional array $students = array( array('name' = >' Zhang San',' age' => 18), array('name' = >' Li Si',' age' => 20), array('name' = >' Wang Wu',' age'. //Delete the first element in the $students array, that is, the information unset($students[0]) of Zhang San; //output the deleted array print_r($students);

In the above code, we use the unset function to delete the first element in the $students array, which is the information of Zhang San. After executing the above code, the output result only contains the remaining three elements.

Second, use the array_splice function

The array_splice function in PHP can modify the contents of an array by deleting or replacing a part of the array. If you need to delete elements in the array, you can set parameter three to 0, and parameter four indicates the number of elements to be deleted. The following is an example of using the array_splice function to delete a specified element in a two-dimensional array:

//Define a two-dimensional array $students = array( array('name' = >' Zhang San',' age' => 18), array('name' = >' Li Si',' age' => 20), array('name' = >' Wang Wu',' age'. //Use the array_splice function to delete the first element in the $students array, that is, the information array_splice($students, 0, 1) of Zhang San; //output the deleted array print_r($students);

In the above code, we use the array_splice function to delete the first element in the $students array, which is the information of Zhang San. After executing the above code, the output result only contains the remaining three elements.

Third, use the foreach loop

If we want to delete elements in a two-dimensional array that meet certain conditions, we can use the foreach loop to achieve it. The following is an example of using the foreach loop to delete elements in a two-dimensional array that meet certain conditions:

//Define a two-dimensional array $students = array( array('name' = >' Zhang San',' age' => 18), array('name' = >' Li Si',' age' => 20), array('name' = >' Wang Wu',' age'. //Use the foreach loop to delete the information foreach ($students as $ key = > $ value) {if ($ value ['age'] < 20) {unset ($ students [$ key]) of students under 20 years old in the $ students array; }}//Output the deleted array print_r($students);

In the above code, we use the foreach loop to traverse all the elements in the $students array, and use the unset function to delete the qualified elements. After executing the above code, the output result only contains three elements aged 20 or older.

In short, there are many ways to delete elements in a two-dimensional array in PHP, just choose the most suitable way according to the actual situation. It should be noted that before array operation, it is best to check the existence of array and the validity of elements to avoid unexpected errors in the program.


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us