469,306 Members | 1,987 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

foreach and multidimensional arrays

Hi.

Does for each for on multidimensional arrays? For example, if one
element of $in is $course_list and I want to echo all everything in
course_list

Jul 17 '05 #1
6 15291
On 27 Jan 2005 15:16:31 -0800, ub****@gmail.com wrote:
Does for each for on multidimensional arrays? For example, if one
element of $in is $course_list and I want to echo all everything in
course_list


foreach operates on arrays, and only to one level; if the array contains
arrays, foreach doesn't care - the loop index will just be set to an array that
is that dimension of the array.

You could use is_array() if you want to tell if you've hit another dimension
of the array.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2
ub****@gmail.com wrote:
Does for each for on multidimensional arrays? For example, if one
element of $in is $course_list and I want to echo all everything in
course_list


No. You'd need to test the values in the array to see if they were also
arrays and do something with that eg

foreach($foo as $index => $value) {
if(is_array($value)) {
... do something ...
}
}

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #3
Hi....

I found the answer...of using nested arrays...
foreach ($in as $key) {
if(is_array($key)){
foreach ($key as $key2 => $value) {

echo "Key: $key; Value: $value<br />\n";

}
} else {

echo "Key: $key; Value: $value<br />\n";

}

}

Jul 17 '05 #4
I noticed that Message-ID:
<11**********************@c13g2000cwb.googlegroups .com> from
ub****@gmail.com contained the following:
I found the answer...of using nested arrays...

What about n dimensional arrays?

I'm thinking perhaps a recursive function?
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #5
"Geoff Berrow" <bl******@ckdog.co.uk> wrote in message
news:ov********************************@4ax.com...
I noticed that Message-ID:
<11**********************@c13g2000cwb.googlegroups .com> from
ub****@gmail.com contained the following:
I found the answer...of using nested arrays...

What about n dimensional arrays?

I'm thinking perhaps a recursive function?


yes.
Jul 17 '05 #6
I noticed that Message-ID: <ct**********@news.tue.nl> from skrebbel
contained the following:
I'm thinking perhaps a recursive function?


yes.


But I'd usually use print_r() if I just wanted to take a peek.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Terry | last post: by
9 posts views Thread by Charles Banas | last post: by
2 posts views Thread by chris | last post: by
3 posts views Thread by Ravi Singh (UCSD) | last post: by
21 posts views Thread by utab | last post: by
22 posts views Thread by J. Frank Parnell | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.