Home  >  Article  >  Backend Development  >  How to delete element code in php array

How to delete element code in php array

PHPz
PHPzOriginal
2023-04-26 09:10:10508browse

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.

  1. The unset function deletes array elements

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']);
  1. array_splice function deletes array elements
# The ##array_splice function is a built-in function in PHP that can be used to delete elements in an array, insert new elements, or replace original elements. This function needs to receive three or four parameters, which are the array to be operated on, the index or key name to start deletion, the number of elements to be deleted and the elements to be inserted (if any).

If you only need to delete elements in the array, you can set the third parameter to 1. If you want to remove multiple elements, set this to the number of elements you need to remove.

Sample code:

//定义一个数字索引数组
$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.

    array_filter function deletes array elements under specific conditions
array_filter is a built-in function in PHP that can filter elements in an array based on specified conditions (callback function) . Therefore, we can delete elements from the array by specifying a callback function that deletes the element.

Sample code:

//定义一个关联数组
$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, &#39;deleteLowScore&#39;);
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.

Summary

The 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!

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