php arrays (call it coding if that helps)

User avatar
Crisium
Level 7 - Spellcaster
Posts: 71
Joined: 13 Mar 2008, 17:57
Location: Odense C
Contact:

php arrays (call it coding if that helps)

Unread post by Crisium » 27 Sep 2008, 17:51

Hi all,

I'm kind of stuck on a php problem:

$a = array();
$a[] = array( "name"=>"peter", "city"=>"odense" );
$a[] = array( "name"=>"lars", "city"=>"copenhagen" );
$a[] = array( "name"=>"bo", "city"=>"aarhus" );

what I want to do is delete the array in $a that has for example a name="peter", so
the whole array that contains name peter is removed from $a

anyone know how this is done?
regards,
Peter
kind regards,
Peter Wraae Marino

http://www.osghelp.com - support site for OpenSceneGraph

rasmuskaae
Level 2 - Grain of sand
Posts: 23
Joined: 11 Mar 2008, 08:56
Contact:

Re: php arrays (call it coding if that helps)

Unread post by rasmuskaae » 30 Sep 2008, 20:19

There is only the "ugly" way of doing this. Run through the array, remember all dirty items and clean up afterwards.

Code: Select all

$dirtyitems = array();
for ($i=0; $i<sizeof($a); $i++)
{
 if ($a[$i][name]=="peter") $dirtyitems[]=$i;
}
foreach ($dirtyitems as $item) unset($a[$i]);
^^ not tested :-)

Post Reply