Page 1 of 1

php arrays (call it coding if that helps)

Posted: 27 Sep 2008, 17:51
by Crisium
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

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

Posted: 30 Sep 2008, 20:19
by rasmuskaae
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 :-)