You are here: Re: how to delete record in an array of arrays ? « PHP Language « IT news, forums, messages
Re: how to delete record in an array of arrays ?

Posted by Rik on 02/15/07 13:52

fabrice régnier <regnier.fab@free.fr> wrote:
> $res= array();
> $res[]=array("name" => "Smith" , "age" => 10);
> $res[]=array("name" => "Johnson" , "age" => 20);
> $res[]=array("name" => "Adam" , "age" => 0);
> $res[]=array("name" => "Sullivan" , "age" => 15);
>
> print_r($res);
>
> Array (
> [0] => Array ( [name] => Smith [age] => 10 )
> [1] => Array ( [name] => Johnson [age] => 20 )
> [2] => Array ( [name] => Adam [age] => 0 )
> [3] => Array ( [name] => Sullivan [age] => 15 )
> )
>
> My question is: i'd like to delete all records that have their age = 0
>
> As a result, i'd like to see :
>
> Array (
> [0] => Array ( [name] => Smith [age] => 10 )
> [1] => Array ( [name] => Johnson [age] => 20 )
> [2] => Array ( [name] => Sullivan [age] => 15 )
> )
>
> I've tried array_filter, array_splice, unset & array_shift but without
> any success. I guess i miss something in the multi-dimensional array
> management.

Either by a loop:
$filtered = array()
foreach ($res as $key => $item){
if($res['age']!=0) $filtered[] = $item;
//or preserve keys by $filtered[$key] = $item;
}

Or indeed by array_filter:
$filtered = array_filter($res,create_funtion('$v,$k','return
$v['age']!=0;'));

Where are you getting the data from by the way? It might be easier to
tackle this at the source.
--
Rik Wasmus

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация