Home > Backend Development > PHP Problem > How to remove specified value from array in php

How to remove specified value from array in php

WBOY
Release: 2023-05-07 14:31:08
Original
633 people have browsed it

PHP is a popular server-side scripting language widely used for web development. In development, it is often necessary to manipulate data, which often involves array operations. When operating an array, we may need to remove some values ​​in the array. This article will explain how to remove specified values ​​from an array in PHP.

1. Use the unset() function

The unset() function in PHP can be used to remove elements at specified positions in an array. The code is as follows:

$items = array('apple', 'banana', 'orange', 'pear');
unset($items[1]); //移除数组中下标为1的元素
print_r($items);
Copy after login

The output result is:

Array
(
    [0] => apple
    [2] => orange
    [3] => pear
)
Copy after login

In the above code, we define an array $items, containing 4 elements: 'apple', 'banana', 'orange ' and 'pear'. Then use the unset() function to remove the element with index 1 in the array, that is, 'banana'. Finally, the print_r() function is used to output the array after removing the elements.

2. Use the array_splice() function

The array_splice() function in PHP can be used to remove elements from an array and insert new elements after removal. If you only need to remove elements without inserting new ones, pass the third parameter as 0. The code is as follows:

$items = array('apple', 'banana', 'orange', 'pear');
array_splice($items, 1, 1); //移除数组中下标为1的元素
print_r($items);
Copy after login

The output result is:

Array
(
    [0] => apple
    [1] => orange
    [2] => pear
)
Copy after login

In the above code, we use the array_splice() function to remove the element with index 1 in the array, that is, 'banana'. Finally, the print_r() function is used to output the array after removing the elements.

3. Use the array_diff() function

The array_diff() function in PHP can be used to calculate the difference of an array and return a new array containing different elements. First create an array containing all the elements you want to remove, then pass it as the second argument and the original array as the first argument. The code is as follows:

$items = array('apple', 'banana', 'orange', 'pear');
$remove_items = array('banana', 'pear');
$items = array_diff($items, $remove_items); //移除数组中指定元素
print_r($items);
Copy after login

The output result is:

Array
(
    [0] => apple
    [1] => orange
)
Copy after login

In the above code, we define an array $items, containing 4 elements: 'apple', 'banana', 'orange ' and 'pear'. We also define an array $remove_items containing the elements to be removed: 'banana' and 'pear'. These elements in the array are then removed using the array_diff() function. Finally, the print_r() function is used to output the array after removing the elements.

Conclusion

This article introduces how to remove specified values ​​from an array in PHP, and provides three common methods: using the unset() function, using the array_splice() function and using array_diff() function. Developers can choose different methods according to the actual situation. No matter which method is chosen, it can help developers better operate arrays and improve code execution efficiency.

The above is the detailed content of How to remove specified value from array in php. 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