Home > Article > Backend Development > How to delete element code in php array
In PHP, arrays are a very useful data type that can be used to store various types of data, and array elements can be accessed through indexes, associated keys, etc. However, sometimes we need to delete some unnecessary elements from the array. In this case, we need to use some methods of deleting array elements in PHP.
This article will introduce several common methods of deleting elements from PHP arrays for readers' reference.
In PHP, you can use the unset function to delete one or more array elements. This function needs to receive one or more parameters, each parameter is the key name or index value of the array element to be deleted.
Sample code:
//定义一个关联数组 $arr = array( 'name' => '张三', 'age' => 18, 'gender' => '男' ); unset($arr['age']); //删除键为'age'的元素 print_r($arr); //输出结果:Array ( [name] => 张三 [gender] => 男 )
In the above code, we first define an associative array $arr, and use the unset function to delete the element with the key 'age'. Finally, use the print_r function to output the result of $arr. You can see that the 'age' element has been deleted.
If you need to delete multiple elements, you can pass in multiple parameters in the unset function, as shown below:
unset($arr['name'], $arr['gender']);
//定义一个数字索引数组 $arr = array('apple', 'banana', 'orange', 'grape', 'peach'); array_splice($arr, 2, 1); //删除从索引2开始的1个元素(即'orange') print_r($arr); //输出结果:Array ( [0] => apple [1] => banana [2] => grape [3] => peach )In the above code, we first define a numeric index array $arr, and use the array_splice function to delete 1 element starting from index 2 (i.e. 'orange') , and then use the print_r function to output the result of the array.
//定义一个关联数组 $arr = array( 'name' => '张三', 'age' => 18, 'gender' => '男', 'score' => 90 ); //定义一个回调函数,删除分数小于80的元素 function deleteLowScore($value, $key) { if($key == 'score' && $value < 80) { return false; } return true; } $arr = array_filter($arr, 'deleteLowScore'); print_r($arr); //输出结果:Array ( [name] => 张三 [age] => 18 [gender] => 男 [score] => 90 )In the above code, we first define an associative array $arr, and then define a callback function named deleteLowScore, which deletes elements with a score lower than 80 . Next, we use the array_filter function to apply the callback function and reassign the result to the array $arr. Finally, use the print_r function to output the result of the array. You can see that elements with scores lower than 80 have been deleted. SummaryThe above are several common ways to delete elements from PHP arrays, including using the unset function, array_splice function and array_filter function. In actual development, choose which method to delete elements according to specific needs. If you need to delete a single element, it is most convenient to use the unset function; if you need to delete multiple elements, the array_splice function may be more suitable; if you need to delete elements based on specific conditions, the array_filter function is the best choice.
The above is the detailed content of How to delete element code in php array. For more information, please follow other related articles on the PHP Chinese website!