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);
The output result is:
Array ( [0] => apple [2] => orange [3] => pear )
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);
The output result is:
Array ( [0] => apple [1] => orange [2] => pear )
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);
The output result is:
Array ( [0] => apple [1] => orange )
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!