Possible Duplicate:
How to delete an element from an array in php?
I have a list of things, cars for instance
$cars[0] = "audi";
$cars[1] = "saab";
$cars[2] = "volvo";
$cars[3] = "vw";
How do i delete "volvo" from the list?
We can remove duplicate element in an array by 2 ways: using temporary array or using separate index. To remove the duplicate element from array, the array must be in sorted order. If array is not sorted, you can sort it by calling Arrays. sort(arr) method.
pop() function: This method is use to remove elements from the end of an array. shift() function: This method is use to remove elements from the start of an array. splice() function: This method is use to remove elements from the specific index of an array.
indexOf() function. The idea is to compare the index of all items in an array with an index of their first occurrence. If both indices don't match for any item in the array, you can say that the current item is duplicated. To return a new array with duplicates, use the filter() method.
$volvoIndex = array_search('volvo', $cars);
unset($cars[$volvoIndex]);
you can do with unset
unset($cars[2]);
But after that you need to iterate array with foreach
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With