This is a discussion on nested array... within the PHP General forums, part of the PHP Programming Forums category; hi, i have a nested array ex: print_r($nestedarray): Array( [0]=>Array([id]=>1 [name]=>name1 [etc]=&...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
hi, i have a nested array
ex: print_r($nestedarray): Array( [0]=>Array([id]=>1 [name]=>name1 [etc]=>etc1) [1]=>Array([id]=>2 [name]=>name2 [etc]=>etc2) [3]=>Array([id]=>3 [name]=>name3 [etc]=>etc3) ) if I want to check whether id=5 is in that $nestedarray, how to do that?!?! i'd really appreciate the help.. thanks in advance.. |
|
|||
|
On Dec 17, 9:48 am, penjahat.ja...@gmail.com ("opo jal") wrote:
> hi, i have a nested array > ex: > print_r($nestedarray): > Array( > [0]=>Array([id]=>1 [name]=>name1 [etc]=>etc1) > [1]=>Array([id]=>2 [name]=>name2 [etc]=>etc2) > [3]=>Array([id]=>3 [name]=>name3 [etc]=>etc3) > ) > > if I want to check whether id=5 is in that $nestedarray, how to do that?!?! > > i'd really appreciate the help.. > > thanks in advance.. foreach($nestedarray as $arr) { if($arr['id'] == 5) echo 'Found id 5'; } |
|
|||
|
$nestedarray[$i['id'] == 5
and $i is your array index On 17/12/2007, opo jal <penjahat.jahat@gmail.com> wrote: > > hi, i have a nested array > ex: > print_r($nestedarray): > Array( > [0]=>Array([id]=>1 [name]=>name1 [etc]=>etc1) > [1]=>Array([id]=>2 [name]=>name2 [etc]=>etc2) > [3]=>Array([id]=>3 [name]=>name3 [etc]=>etc3) > ) > > if I want to check whether id=5 is in that $nestedarray, how to do > that?!?! > > i'd really appreciate the help.. > > thanks in advance.. > -- Best Regards Cesar D. Rodas http://www.cesarodas.com http://www.thyphp.com http://www.phpajax.org Phone: +595-961-974165 |
|
|||
|
> print_r($nestedarray):
> Array( > [0]=>Array([id]=>1 [name]=>name1 [etc]=>etc1) > [1]=>Array([id]=>2 [name]=>name2 [etc]=>etc2) > [3]=>Array([id]=>3 [name]=>name3 [etc]=>etc3) > ) > > if I want to check whether id=5 is in that $nestedarray, how to do that?!?! > > i'd really appreciate the help.. <?php foreach ($nestedarray as $v) { if ($v['id'] == 5) { $in_array = true; break; } } ?> -- Richard Heyes http://www.websupportsolutions.co.uk Knowledge Base and HelpDesk software that can cut the cost of online support ** NOW OFFERING FREE ACCOUNTS TO CHARITIES AND NON-PROFITS ** |
|
|||
|
On Mon, December 17, 2007 8:48 am, opo jal wrote:
> hi, i have a nested array > ex: > print_r($nestedarray): > Array( > [0]=>Array([id]=>1 [name]=>name1 [etc]=>etc1) > [1]=>Array([id]=>2 [name]=>name2 [etc]=>etc2) > [3]=>Array([id]=>3 [name]=>name3 [etc]=>etc3) > ) > > if I want to check whether id=5 is in that $nestedarray, how to do > that?!?! > > i'd really appreciate the help.. I would have built the arrays with the ID as the index in the first place, and then used http://php.net/isset, but maybe that's just me... You could mess with in_array I suspect, or write an array_map that stores the ID when you hit it... -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/from/lynch Yeah, I get a buck. So? |