Remove multiple elements from array php
I have a dynamically generated array of filenames, let's say it looks something like this: Show
I have a couple of specific filenames that I want discarded from the array:
So, I'll always know the values of the elements I want discarded, but not the keys. Currently I'm looking at a couple of ways to do this. One way, using array_filter and a custom function:
Another way, using array_keys and unset:
Both ways produce the desired result. I'm just wondering which one would be more efficient (and why)? Or perhaps there is another, more efficient way to do this? Like maybe there's a way to have multiple search values in the array_search function? Today’s article, we will remove multiple elements by key in a PHP array. Using PHP we can delete multiple keys from an Array. In PHP there is no built in method to achieve this, but we can combine several PHP functions to achieve this. In this article we will be demonstrating two methods to remove multiple keys from PHP array. Method 1 : unset methodBelow code we are removing two keys (hobby, like) from the array using unset method $customer = [ 'name'=>'John Doe', 'email'=>'[email protected]', 'street'=>'Street 1, Avenue Drive', 'city'=>'Texas', 'hobby' => 'Reading', 'country' => 'US', 'like' => 'Fishing' ]; $toRemove = ['hobby', 'like']; foreach($toRemove as $key){ unset($customer[$key]); } print_r($customer); Method 2 Using combination of array_diff_key and array_flip$removeFields = ['hobby', 'like']; $newCustomer = array_diff_key($customer, array_flip($removeFields)); print_r($newCustomer); Let’s say you have an array and you want to delete one or more elements from that array using PHP. Maybe you have to delete an element with a specific value or maybe you have to delete multiple consecutive elements. In this article, you will learn about different inbuilt PHP functions that can help you remove one or more elements based on their key or value. Here are a few variables that we are going to use: PHP I have
used the short array syntax in the code above which replaces The easiest way to remove or delete a single element from an array in PHP is to use the unset() function. This function is
used to unset a given variable. In other words, you can use it to destroy any variable in your code. To remove an array element by its key, provide the PHP
You should note that the keys of both the arrays remain unchanged after deleting the element from the arrays. If you want to reindex the keys after removing the element so that there is no gap, you can use the array_values() function. This function returns an indexed array of all the values in the supplied array. Remember that PHP
Remove an Element from an Array by its Value Using unset()The only problem with the above solution is that you actually need to know the key to delete an element. Let’s say you only know the value of the element that you want to delete and have no idea about its key. In such cases, you can first use the
array_search() function in PHP to get the key for a given value and then use PHP
As you saw in the above example, you can use Using array_splice() to Delete Multiple Consecutive Elements from an ArrayThe array_splice() function in PHP is used to remove a portion of an array and replace it with something else. However, providing the replacement is optional and you can use this function to remove a portion of the array in one go. Here is an example: PHP
Besides removing multiple elements at once, another advantage of this method is that the numeric keys were automatically re-indexed. At the same time, the keys of the associative array remained unchanged. One important point that you should remember is that you have to pass the offset as second parameter and not the key. That’s why we did not pass the key Using array_diff() to Delete Multiple Elements by Their ValueWhile What if you want to delete array elements by their value and those elements are not placed sequentially? The array_diff() function in PHP proves very useful in this situation. This function is used to remove all the elements in the first array that are also present in second array. In other words, you can use this method to remove non-consecutive array elements by their value with ease. You should note that the keys in second array do not matter. Only the values need to match for removal of elements. PHP
As you can see in the above example, you can use this technique to remove multiple elements by their value from numeric as well as associative arrays. One thing that I would like to point out is that the keys in the second array don’t have to match the first array for deletion to be successful. Using array_diff_key() to Delete Multiple Array Elements by Their KeysJust like you use PHP
In the above
example, the output of the first Quick SummaryLet’s recap everything that we have covered in this tutorial.
Let me know if there is anything that you would like me to clarify. Also, you are more than welcome to comment if you know other techniques to delete one or more elements from an array in PHP. Rate this post — Loading...How to delete multiple values from array in PHP?Another way, using array_keys and unset: $exclude_files_keys = array(array_search($exclude_file_1,$files),array_search($exclude_file_2,$files)); foreach ($exclude_files_keys as $exclude_files_key) { unset($files[$exclude_files_key]); } $files = array_values($page_file_paths);
How do I remove multiple elements from an array?Approach 1: Store the index of array elements into another array which need to be removed. Start a loop and run it to the number of elements in the array. Use splice() method to remove the element at a particular index.
How to remove some elements from array in PHP?In order to remove an element from an array, we can use unset() function which removes the element from an array and then use array_values() function which indexes the array numerically automatically.
How to remove all data from array in PHP?Answer: Use the PHP unset() Function
If you want to delete an element from an array you can simply use the unset() function. The following example shows how to delete an element from an associative array and numeric array.
|