This question has several answers but I want to add something more because when I used unset
or array_diff
I had several problems to play with the indexes of the new array when the specific element was removed [because the initial index are saved]
I get back to the example :
$array = array['apple', 'orange', 'strawberry', 'blueberry', 'kiwi'];
$array_without_strawberries = array_diff[$array, array['strawberry']];
or
$array = array['apple', 'orange', 'strawberry', 'blueberry', 'kiwi'];
unset[$array[array_search['strawberry', $array]]];
If you print the result you will obtain :
foreach [$array_without_strawberries as $data] {
print_r[$data];
}
Result :
> apple
> orange
> blueberry
> kiwi
But the indexes will be saved and so you will access to your element like :
$array_without_strawberries[0] > apple
$array_without_strawberries[1] > orange
$array_without_strawberries[3] > blueberry
$array_without_strawberries[4] > kiwi
And so the final array are not re-indexed. So you need to add after the unset
or array_diff
:
$array_without_strawberries = array_values[$array];
After that your array will have a normal index :
$array_without_strawberries[0] > apple
$array_without_strawberries[1] > orange
$array_without_strawberries[2] > blueberry
$array_without_strawberries[3] > kiwi
Related to this post : Re-Index Array
Hope it will help
Description:
You need to write a program in PHP to remove specific element by value from an array using PHP program.
Instructions:
- Take an array with list of month names.
- Take a variable with the name of value to be deleted.
- You can use PHP
array
functions orforeach
loop.
Solution 1: Using array_search[]
With the help of array_search[]
function, we can remove specific elements from an array.
Tutorials Class - Output Window
array[4] { [0]=> string[3] “jan” [1]=> string[3] “feb” [3]=> string[5] “april” [4]=> string[3] “may” }
Solution 2: Using foreach[]
By using foreach[]
loop, we can also remove specific elements from an array.
Tutorials Class - Output Window
array[4] { [0]=> string[3] “jan” [1]=> string[3] “feb” [3]=> string[5] “april” [4]=> string[3] “may” }
Solution 3: Using array_diff[]
With the help of array_diff[]
function, we also can remove specific elements from an array.
Tutorials Class - Output Window
array[4] { [0]=> string[3] “jan” [1]=> string[3] “feb” [2]=> string[5] “march” [4]=> string[3] “may” }
Learn more about the similar topics:
PHP Arrays |
Remove specific element by value from an array in PHP? |
PHP Array to String Conversion [favourite colours chosen by user] |
How to check if an array is a subset of another in PHP? |
No Content Found. |
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
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.
Function Used:
- unset[]: This function unsets a given variable.
Syntax:void unset [ mixed $var [, mixed $... ] ]
- array_values[]: This function returns all the values from the array and indexes the array
numerically.
Syntax:array array_values [ array $array ]
Example 1:
Output:
array[2] { [0]=> string[5] "geeks" [2]=> string[5] "geeks" } array[2] { [0]=> string[5] "geeks" [2]=> string[5] "geeks" }
We can also
use array_splice[] function which removes a portion of the array and replaces it with something else.
Example 2:
Output:
array[2] { [0]=> string[5] "geeks" [1]=> string[5] "geeks" }
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.