TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-25-2009, 06:51 PM   #1 (permalink)
The Wanderer
 
Join Date: Jun 2009
Posts: 20
Thanks: 2
tech is on a distinguished road
Default 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..
tech is offline  
Reply With Quote
Old 06-25-2009, 07:00 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-25-2009, 07:07 PM   #3 (permalink)
The Wanderer
 
Join Date: Jun 2009
Posts: 20
Thanks: 2
tech is on a distinguished road
Default

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.
tech is offline  
Reply With Quote
Old 06-25-2009, 07:11 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
tech (06-25-2009)
Old 06-25-2009, 07:51 PM   #5 (permalink)
The Wanderer
 
Join Date: Jun 2009
Posts: 20
Thanks: 2
tech is on a distinguished road
Default

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".
tech is offline  
Reply With Quote
Old 06-25-2009, 07:58 PM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-25-2009, 09:19 PM   #7 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by tech View Post
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
)
Salathe is offline  
Reply With Quote
Old 06-25-2009, 09:35 PM   #8 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-25-2009, 09:41 PM   #9 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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); 
Salathe is offline  
Reply With Quote
Old 06-25-2009, 09:48 PM   #10 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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);
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to delete array element in function benton General 4 04-02-2009 01:12 AM
How to search array benton General 3 03-29-2009 12:31 AM
Array mess Killswitch Absolute Beginners 4 12-14-2008 07:35 AM
array elements into variables and values Dave Absolute Beginners 7 06-20-2008 01:56 PM
Part 1: Getting Started with Array Functions Wildhoney Absolute Beginners 6 10-01-2007 10:53 AM


All times are GMT. The time now is 09:27 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design