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