![]() |
how to delete an element from an array without knowing its index position
dear friends..
Any one please help me to write a code to delete an element from an array where i don't know the index position of an element.and i wanted to use array_splice if possible. example: $x=array("a","b","c","d"); array_splice($a,2,1);//if i want to delete "b" from an array print_r($x); here i know about the position but if i don't know about the position of an element then how can i do? thanks.. |
Do you know what the item in the array should contain?
|
don't know about the element is present in an array.I have to search the element in that array and its position then delete it.
|
I wrote the following two functions, without knowing what you want precisely, admittedly, but I had a sudden urge to write them!
The first of the two functions is the most simply of them, and will remove only the first instance of "Apples" it finds. The second function will remove every single "Apples" instance it can possibly find in the array. I am posting both, and not combining them into one function, so that you're able to easily under the most basic of the two functions: array_drop_singlearray_drop_singlephp Code:
array_drop_multiplephp Code:
However, ignoring the latter of the two functions, this is how you would interact with array_drop_single. The array is passed as reference, and so is changed literally inside the function itself. There is no return value in either of the functions. |
Thanks dear..it will work but if i want to delete a multiple element from an array which is not same then what i can do?
example: array("a","b","c","d"); and i wanted to delete "b" and after that element .but i don't know the position of "b". |
It's a good job I am in the mood for arrays :-) !
We can then modify the two functions a little bit to accommodate for arrays to be passed in. It will still work the same as before if you want to remove a single item. However, we now have the ability to remove multiple indexes by passing in an array of the values to be removed. Although before we could have just repeated the call to array_drop_* many times.array_drop_multiplephp Code:
array_drop_singlephp Code:
Then to use the functions, in this case I am using the array_drop_multiple function as the example:php Code:
Result: Quote:
php Code:
|
Quote:
PHP Code:
Code:
Array |
Yes. That's another way to do it, which is pretty much as below. Although I added them into a function for him.
php Code:
Then he also wanted to delete all the items whilst supplying many values, too. Which is just a loop inside a loop: php Code:
Unless array_keys allows you to pass in an array, then you're also going to need a loop inside a loop. |
That's even easier then (again unless I'm missing something).
PHP Code:
|
I prefer that way using
array_diff. It is a handy function. Otherwise either array_keys or array_search works just as well as one another.Was it coincidence that your array outputs as ABBA? :-) One thing somebody might want though is to rebuild the indexes once some items have been removed. Therefore all the indexes now increment nicely again. php Code:
|
| All times are GMT. The time now is 09:37 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0