 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
06-25-2009, 06:51 PM
|
#1 (permalink)
|
|
The Wanderer
Join Date: Jun 2009
Posts: 20
Thanks: 2
|
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..
|
|
|
|
06-25-2009, 07:00 PM
|
#2 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
Do you know what the item in the array should contain?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
06-25-2009, 07:07 PM
|
#3 (permalink)
|
|
The Wanderer
Join Date: Jun 2009
Posts: 20
Thanks: 2
|
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.
|
|
|
|
06-25-2009, 07:11 PM
|
#4 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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_single
array_drop_single
php Code:
/** * Example: Removes only the first "Apples" it locates. * * @return void * @param string $szSearch * @param array $aArray */function array_drop_single ($szSearch, & $aArray){ $iKey = array_search($szSearch, $aArray); unset($aArray[ $iKey] ); }
array_drop_multiple
php Code:
/** * Example: Removes all the "Apples" from the array. * array('Oranges', 'Apples', 'Apples'); * (This will leave only "Oranges".) * * @return void * @param string $szSearch * @param array $aArray */function array_drop_multiple ($szSearch, & $aArray){ while (($iKey = array_search($szSearch, $aArray)) !== false) { unset($aArray[ $iKey] ); }}
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.
php Code:
$aFruits = array('Oranges', 'Apples', 'Pears', 'Bananas'); /* Let's shuffle the array so we can't cheat. */shuffle($aFruits); /* Drop an item array based on its value. */array_drop_single ('Apples', $aFruits); /* Let's see if it's gone... */print_r($aFruits); /* ...Has it? */
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Last edited by Wildhoney : 06-25-2009 at 07:33 PM.
|
|
|
|
The Following User Says Thank You to Wildhoney For This Useful Post:
|
|
06-25-2009, 07:51 PM
|
#5 (permalink)
|
|
The Wanderer
Join Date: Jun 2009
Posts: 20
Thanks: 2
|
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".
|
|
|
|
06-25-2009, 07:58 PM
|
#6 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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_multiple
php Code:
/** * Example: Removes all the "Apples" from the array. * array('Oranges', 'Apples', 'Apples'); * (This will leave only "Oranges".) * * @return void * @param mixed $szSearch * @param array $aArray */function array_drop_multiple ($mSearch, & $aArray){ if (! is_array($mSearch)) { $mSearch = (array) $mSearch; } foreach ($mSearch as $szSearch) { while (($iKey = array_search($szSearch, $aArray)) !== false) { unset($aArray[ $iKey] ); } }}
array_drop_single
php Code:
/** * Example: Removes only the first "Apples" it locates. * * @return void * @param mixed $szSearch * @param array $aArray */function array_drop_single ($mSearch, & $aArray){ if (! is_array($mSearch)) { $mSearch = (array) $mSearch; } foreach ($mSearch as $szSearch) { $iKey = array_search($szSearch, $aArray); unset($aArray[ $iKey] ); }}
Then to use the functions, in this case I am using the array_drop_multiple function as the example:
php Code:
$aFruits = array('Oranges', 'Apples', 'Pears', 'Bananas', 'Apples'); /* Let's shuffle the array so we can't cheat. */shuffle($aFruits); /* Drop an item array based on its value. */array_drop_multiple (array('Apples', 'Bananas'), $aFruits); /* Let's see if it's gone... */print_r($aFruits); /* ...Has it? */
Result:
Quote:
Array
(
[3] => Oranges
[4] => Pears
)
|
Notice the array being passed in at the first argument is now an array. This removes multiple items. Although we can still pass in a single item, like so:
php Code:
/* Drop an item array based on its value. */ array_drop_multiple('Apples', $aFruits);
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
06-25-2009, 09:19 PM
|
#7 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Quote:
Originally Posted by tech
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
|
Maybe I'm missing something (which is quite possible!) but here is a way to delete an element from an array by specifying the value (not the index) to be deleted. It'll happily delete multiple elements if there are more than one of the specified value.
PHP Code:
$array = array('a', 'b', 'c', 'd');
foreach (array_keys($array, 'b', TRUE) as $key)
{
unset($array[$key]);
}
print_r($array);
Which outputs something like:
Code:
Array
(
[0] => a
[2] => c
[3] => d
)
|
|
|
|
06-25-2009, 09:35 PM
|
#8 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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:
while (($iKey = array_search($szSearch, $aArray)) !== false){ unset($aArray[ $iKey] ); }
Then he also wanted to delete all the items whilst supplying many values, too. Which is just a loop inside a loop:
php Code:
foreach ($mSearch as $szSearch){ while (($iKey = array_search($szSearch, $aArray)) !== false) { unset($aArray[ $iKey] ); }}
Unless array_keys allows you to pass in an array, then you're also going to need a loop inside a loop.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
06-25-2009, 09:41 PM
|
#9 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
That's even easier then (again unless I'm missing something).
PHP Code:
$array = array('a', 'b', 'c', 'd', 'c', 'b', 'a'); $remove = array('c', 'd'); // or just, 'c' $array = array_diff($array, (array) $remove); print_r($array);
|
|
|
|
06-25-2009, 09:48 PM
|
#10 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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:
$array = array('a', 'b', 'c', 'd', 'c', 'b', 'a'); $remove = array('c', 'd', 'f', 'Bleh'); // or just, 'c'$array = array_diff($array, (array) $remove); $array = array_values($array); // restores the indexesprint_r($array);
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|