For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > February 2007 > how to delete record in an array of arrays ?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to delete record in an array of arrays ?
fabrice régnier

2007-02-15, 8:00 am

Hi all ;)

Here is my array of arrays:

$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.

Thanx in advance ;)

f.
Rik

2007-02-15, 8:00 am

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
fabrice régnier

2007-02-15, 6:59 pm

hi ;)

> 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;'));


I've tested your 2 solutions and both doesn't work.

First solution gives me an empty array as a result.
Second solution gives me a syntaxic error.

Maybe you forgot something ? Thanx, anyway.

f.

Rik

2007-02-15, 6:59 pm

fabrice régnier <regnier.fab@free.fr> wrote:
>
> I've tested your 2 solutions and both doesn't work.
>
> First solution gives me an empty array as a result.
> Second solution gives me a syntaxic error.
>
> Maybe you forgot something ? Thanx, anyway.



Allthough I usually respond with a lot of code, I do not test this. It's
an insight in how you can solve your problem, if you want me to write your
code for you you'll have to pay the usual rate. Getting an answer is not
like paying for a full working code, you'll have to do your own
error-hunting and implementation. I suggest some reading material:
<http://www.catb.org/~esr/faqs/smart-questions.html>.

FYI: What was wrong?
First example:
if($res['age']!=0) $filtered[] = $item;
should be
if($item['age']!=0) $filtered[] = $item;

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

Both mistakes are easily spotted with a minimum amount of effort.
--
Rik Wasmus
Richard

2007-02-15, 6:59 pm

Rik <luiheidsgoeroe@hotmail.com> writes:

> fabrice régnier <regnier.fab@free.fr> wrote:
>
>
> Allthough I usually respond with a lot of code, I do not test
> this. It's an insight in how you can solve your problem, if you want
> me to write your code for you you'll have to pay the usual
> rate. Getting an answer is not like paying for a full working code,
> you'll have to do your own error-hunting and implementation. I
> suggest some reading material:
> <http://www.catb.org/~esr/faqs/smart-questions.html>.
>
> FYI: What was wrong?
> First example:
> if($res['age']!=0) $filtered[] = $item;
> should be
> if($item['age']!=0) $filtered[] = $item;
>
> Example:
> $filtered = array_filter($res,create_funtion('$v,$k'
,'return
> ($v['age']!=0);'));
>
> Both mistakes are easily spotted with a minimum amount of effort.


If it's not a beginner asking beginner's questions .....
Rik

2007-02-15, 6:59 pm

On Thu, 15 Feb 2007 16:40:51 +0100, Richard <rgrdev@gmail.com> wrote:
>
> If it's not a beginner asking beginner's questions .....


A mistakingly named variable and an unescaped quote? How much of a
beginner do you have to be?

--
Rik Wasmus
Michael

2007-02-15, 6:59 pm


>$filtered = array_filter($res,create_funtion('$v,$k'
,'return
>$v['age']!=0;'));

Well, to begin with the quotes aren't escaped.
Secondly, I'm wondering what sort of object is created by create_funtion.
I'd say, a funtion. But is that similar to a function in any way?


Rik

2007-02-15, 6:59 pm

Michael <mischa.tbaINSERTATHERExs4all.nl> wrote:
>
> Well, to begin with the quotes aren't escaped.


Well, the error was that there were unescaped quotes, as I in answer to =
=

the post saying it did not work.

> Secondly, I'm wondering what sort of object is created by create_funti=

on.
> I'd say, a funtion. But is that similar to a function in any way?


Yes, it is totally similar to a function in almost every possible way, =

allthough functions created by create_function() cannot return a value b=
y =

reference. The function is just anonymous as you're probably not going t=
o =

use it again.

In this example, you could also have done:

function check_age($v,$k){
return ($v['age']!=3D0);
}
$filtered =3D array_filter($array,'check_age');

This would clutter the declared functions with one you probably will not=
=

use again, with a name that may be hard to come up with, and possible =

collisions with another fucntion should you (or someone else) want to =

declare a function with the same name.

Also possible would have been:
$callback =3D create_function('$v,$k','return ($v['age']!=3D0);');
$filtered =3D array_filter($array,$callback);

Which is essentially what we've done, only split for readability, and as=
=

we capture the given name, it's possible to use the custom function agai=
n. =

This would work for instance:

<?php
$callback =3D create_function('','print("I can be called anonymously");'=
);
$callback();
?>
-- =

Rik Wasmus
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com