Home >Backend Development >PHP Problem >How to delete values from php array
In PHP, array is a very useful data structure. We can use arrays to store different values and perform various operations on arrays. Sometimes we need to remove one or more values from an array. PHP provides some built-in functions to help us achieve this goal.
In the following article, we will learn how to delete one or more values from an array using PHP’s built-in functions.
PHP’s unset function can be used to delete the specified key value in the array. Its syntax is as follows:
unset($array[key]);
Among them, $array is the array of key values to be deleted, and key is the key value to be deleted.
For example, we have an array $fruits, which contains the names of some fruits:
$fruits = array("apple", "banana", "orange", "grape");
Now, assuming we want to delete "orange" in this array, we can use the unset function:
unset($fruits[2]);
After running the above code, "orange" in the $fruits array will be deleted.
You can use a loop structure to delete multiple values in an array:
$fruits = array("apple", "banana", "orange", "grape"); $remove = array("banana", "grape"); foreach ($fruits as $key => $value) { if (in_array($value, $remove)) { unset($fruits[$key]); } }
In the above example, we defined an array $remove that contains the values we want to delete from the $fruits array . We use a foreach loop to iterate over each element in the $fruits array and if the value of the current element is in the $remove array, remove it using the unset function.
PHP’s array_splice function can be used to delete specified elements in an array and return the deleted elements. Its syntax is as follows:
array_splice($array, $offset, $length);
Among them, $array is the array of elements to be deleted, $offset is the starting position of the elements to be deleted, and $length is the number of elements to be deleted.
For example, we have an array $colors, which contains some colors:
$colors = array("red", "green", "blue", "yellow", "purple");
Now, suppose we want to remove "green" and "blue" from this array, we can use the array_splice function :
$removed = array_splice($colors, 1, 2);
After running the above code, "green" and "blue" in the $colors array will be deleted, and the two deleted elements will be returned in the $removed variable.
PHP’s array_filter function can use a callback function to filter elements in an array and return a new array. We can use array_filter function to remove certain values from an array. Its syntax is as follows:
array_filter($array, $callback);
Among them, $array is the array to be filtered, and $callback is a callback function used to determine whether the array elements should be retained.
For example, we have an array $numbers, containing some integers:
$numbers = array(1, 2, 3, 4, 5);
Now, assuming we want to remove even numbers in the $numbers array, we can use the array_filter function:
$numbers = array_filter($numbers, function($value) { return $value % 2 != 0; });
In the above example, we use an anonymous callback function to determine whether the array element is an odd number. If it is an odd number, keep the element; otherwise, delete it.
Summary
In PHP, we can use the unset function, array_splice function and array_filter function to delete values in the array in various ways. We should choose the appropriate method according to our needs. At the same time, we should also pay attention to changes in the array key values to ensure that the array is still consistent and continuous.
The above is the detailed content of How to delete values from php array. For more information, please follow other related articles on the PHP Chinese website!