nested array...

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]=&...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-17-2007
opo jal
 
Posts: n/a
Default nested array...

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

Reply With Quote
  #2 (permalink)  
Old 12-17-2007
ZeldorBlat
 
Posts: n/a
Default Re: nested array...

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';
}
Reply With Quote
  #3 (permalink)  
Old 12-17-2007
Cesar D. Rodas
 
Posts: n/a
Default Re: [PHP] nested array...

$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

Reply With Quote
  #4 (permalink)  
Old 12-17-2007
Richard Heyes
 
Posts: n/a
Default Re: [PHP] nested array...

> 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 **
Reply With Quote
  #5 (permalink)  
Old 12-17-2007
Richard Lynch
 
Posts: n/a
Default Re: [PHP] nested array...

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?
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 01:24 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0