TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   how to delete an element from an array without knowing its index position (http://www.talkphp.com/absolute-beginners/4616-how-delete-element-array-without-knowing-its-index-position.html)

tech 06-25-2009 06:51 PM

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..

Wildhoney 06-25-2009 07:00 PM

Do you know what the item in the array should contain?

tech 06-25-2009 07:07 PM

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.

Wildhoney 06-25-2009 07:11 PM

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? */

tech 06-25-2009 07:51 PM

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".

Wildhoney 06-25-2009 07:58 PM

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);

Salathe 06-25-2009 09:19 PM

Quote:

Originally Posted by tech (Post 26063)
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
)


Wildhoney 06-25-2009 09:35 PM

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.

Salathe 06-25-2009 09:41 PM

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); 


Wildhoney 06-25-2009 09:48 PM

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 indexes
print_r($array);


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