" symbol. The sample code is as follows:"/> " symbol. The sample code is as follows:">

Home  >  Article  >  Backend Development  >  How to delete a field in an array in php

How to delete a field in an array in php

PHPz
PHPzOriginal
2023-04-26 10:20:58648browse

PHP is a widely used programming language used for many applications such as web development, server-side programming, and command-line scripting. In PHP, operating on arrays is a very common operation, and deleting a field is also a frequently used operation. In this article, we will explain how to delete a field from an array in PHP.

First, let's look at how to create an array. In PHP, the method to create an array is very simple, just use a pair of square brackets, and the keys and values ​​of the array are separated by the "=>" symbol.

The sample code is as follows:

$arr = array(
    "name" => "Tom",
    "age" => 22,
    "gender" => "male"
);

In the above code, we created an array named $arr, which contains the three key values ​​​​of "nmae", "age" and "gender" right. Next, we will introduce how to delete one of the fields.

Method 1: Use the unset function

The unset() function is a PHP built-in function that can be used to delete an element in an array. When using this function, you need to specify the key name of the element to be deleted. As shown below:

unset($arr["gender"]); // 删除数组$arr中的"gender"元素

In the above code, we use the unset() function to delete the "gender" element in the array $arr. At this point, the array no longer contains the "gender" element.

Method 2: Use the array_filter function

The array_filter() function is also a PHP built-in function that can be used to filter (filter) the array, which can filter out empty elements and specified elements. Therefore, we can filter out specified elements by setting a callback function. An example is as follows:

$arr = array_filter($arr, function($key) {
    return $key !== "gender";
}, ARRAY_FILTER_USE_KEY);

In the above code, we use the array_filter() function and specify a callback function, which is used to determine whether the array elements need to be filtered. In the callback function, we use the "!=" operator to determine whether the key name of the element is equal to "gender". If it is not equal, return true, indicating that the element does not need to be filtered; otherwise, return false, indicating that it needs to be filtered out. Finally, in the array_filter() function, we use the ARRAY_FILTER_USE_KEY option, indicating that we want to use the key name to filter.

Summary

In this article, we introduced two methods to delete specified fields in an array in PHP: using the unset() function and using the array_filter() function. The method of using the unset() function is relatively simple. You only need to specify the key name of the element to be deleted. The method of using the array_filter() function is slightly more complicated. You need to set a callback function to determine whether the array elements need to be filtered. No matter which method is used, it is convenient to delete the specified fields in the array.

The above is the detailed content of How to delete a field in an array in php. 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