Home > Backend Development > PHP Problem > php remove array field

php remove array field

WBOY
Release: 2023-05-11 10:31:37
Original
533 people have browsed it

Arrays in PHP are very powerful and flexible data types that allow us to store and operate large amounts of data, but sometimes we need to delete certain fields in the array. Here are some ways to remove array fields.

Method 1: unset function

The unset function in PHP can be used to delete elements in an array. We can use the unset function to delete a specified array field.

For example, we have an array named $fruit, which contains some fruits and their prices.

$fruit = array("apple"=>2,"banana"=>1,"orange"=>4,"grape"=>3);
Copy after login
Copy after login
Copy after login

Now we want to delete the element corresponding to the 'banana' key value. We can use the unset function to delete it.

unset($fruit['banana']);
Copy after login

Now the array will become like this:

Array
(
    [apple] => 2
    [orange] => 4
    [grape] => 3
)
Copy after login
Copy after login
Copy after login

Method 2: array_diff_key function

The array_diff_key function can compare the key values ​​of two arrays and return the difference set. We can use it to delete specified array fields.

For example, we have an array named $fruit, which contains some fruits and their prices.

$fruit = array("apple"=>2,"banana"=>1,"orange"=>4,"grape"=>3);
Copy after login
Copy after login
Copy after login

Now we want to delete the element corresponding to the 'banana' key value. We can create an array containing only 'banana' and use array_diff_key function to delete it.

$delete = array('banana' => '');
$result = array_diff_key($fruit, $delete);
Copy after login

Now the array will become like this:

Array
(
    [apple] => 2
    [orange] => 4
    [grape] => 3
)
Copy after login
Copy after login
Copy after login

Method 3: array_filter function

The array_filter function can filter useless elements in the array. We can use it to delete specified array fields.

For example, we have an array named $fruit, which contains some fruits and their prices.

$fruit = array("apple"=>2,"banana"=>1,"orange"=>4,"grape"=>3);
Copy after login
Copy after login
Copy after login

Now we want to delete the element corresponding to the 'banana' key value. We can use the array_filter function to filter out this element.

$res = array_filter($fruit, function($key) {
  return ('banana' !== $key);
}, ARRAY_FILTER_USE_KEY);
Copy after login

Now the array will become like this:

Array
(
    [apple] => 2
    [orange] => 4
    [grape] => 3
)
Copy after login
Copy after login
Copy after login

Summary

The above three methods can be used to delete specified fields in the array. Using the unset function is the most common method, but if you want to delete multiple fields, it will be more convenient to use the array_diff_key or array_filter function. Which method you choose depends on your specific use case and personal preference.

The above is the detailed content of php remove array field. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template