469,267 Members | 909 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

How to print a multidimensional associative array?

I have the following associative array:

$user["john"]["mozilla"]=1;
$user["john"]["xmms"]=1;
$user["doe"]["mozilla"]=0;
$user["doe"]["office"]=1;
$user["paul"]["mozilla"]=0;
$user["paul"]["xmms"]=1;
$user["paul"]["office"]=1;

How can I print such an associative array using a loop statement like foreach??
Jul 17 '05 #1
21 18527
JDS
On Tue, 15 Feb 2005 09:40:25 -0800, Leonardo wrote:
How can I print such an associative array using a loop statement like foreach??


Hmmm...

Look it up

http://us3.php.net/foreach

--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Jul 17 '05 #2
NC
Leonardo wrote:

I have the following associative array:

$user["john"]["mozilla"]=1;
$user["john"]["xmms"]=1;
$user["doe"]["mozilla"]=0;
$user["doe"]["office"]=1;
$user["paul"]["mozilla"]=0;
$user["paul"]["xmms"]=1;
$user["paul"]["office"]=1;

How can I print such an associative array using a loop statement
like foreach??


By nesting foreach() statements:

foreach ($user as $name) {
if (is_array($name)) {
foreach ($name as $application) {
echo $application;
}
} else {
echo $name;
}
}

However, if you only need to print it for debugging purposes,
print_r($user) would suite you just fine. Also, you might
want to take a look at var_dump() and var_export():

http://www.php.net/print_r
http://www.php.net/var_dump
http://www.php.net/var_export

Cheers,
NC

Jul 17 '05 #3
"Leonardo" <le*************@gmail.com> wrote in message
news:a3*************************@posting.google.co m...
I have the following associative array:

How can I print such an associative array using a loop statement like

foreach??

you may as well try print_r like in

echo "<pre>";
print_r($user);
echo "</pre>";

rush
--
http://www.templatetamer.com/
http://www.folderscavenger.com/
Jul 17 '05 #4
Thaks everybody for all thoses suggestions.

I said that I need to print. But actually I need to process every data
on this associative array. That's why print_r doesn't work for me.

So, here is the associative array again,

$user["john"]["mozilla"]=1;
$user["john"]["xmms"]=1;
$user["doe"]["mozilla"]=0;
$user["doe"]["office"]=1;
$user["paul"]["mozilla"]=0;
$user["paul"]["xmms"]=1;
$user["paul"]["office"]=1;

I need the following output in my foreach statement:

john mozilla 1
john xmms 1
doe mozilla 0
doe office 1
etc...

If I got the above output from my associative array, I will be able to
genarate the statistics I need, processing each element (instead of
just printing them).
Jul 17 '05 #5
"Leonardo" <le*************@gmail.com> wrote in message
news:a3*************************@posting.google.co m...
Thaks everybody for all thoses suggestions.

I said that I need to print. But actually I need to process every data
on this associative array. That's why print_r doesn't work for me.

So, here is the associative array again,

$user["john"]["mozilla"]=1;
$user["john"]["xmms"]=1;
$user["doe"]["mozilla"]=0;
$user["doe"]["office"]=1;
$user["paul"]["mozilla"]=0;
$user["paul"]["xmms"]=1;
$user["paul"]["office"]=1;

I need the following output in my foreach statement:

john mozilla 1
john xmms 1
doe mozilla 0
doe office 1
etc...

If I got the above output from my associative array, I will be able to
genarate the statistics I need, processing each element (instead of
just printing them).


To expand on NC's example in your format:

foreach ($user as $name => $apps) {
if(is_array($apps)) {
foreach ($apps as $app => $val) {
echo "$name $app $val";
}
} else {
echo $name;
}
}

--
Anonymous

"Treat your password like your toothbrush.
Don't let anybody else use it, and get
a new one every six months."
---Clifford Stoll
Jul 17 '05 #6

"Leonardo" <le*************@gmail.com> wrote in message
news:a3*************************@posting.google.co m...
Thaks everybody for all thoses suggestions.
$user["doe"]["office"]=1;
$user["paul"]["mozilla"]=0;
$user["paul"]["xmms"]=1;
$user["paul"]["office"]=1;

I need the following output in my foreach statement:

john mozilla 1
john xmms 1
doe mozilla 0
doe office 1
etc...

If I got the above output from my associative array, I will be able to
genarate the statistics I need, processing each element (instead of
just printing them).


OK, I give up - here's the fish.

foreach ($user as $fname=>$stats)
{
foreach ($stats as $item=>$value)
{
print "$user -> $item = $value\n";
}
}

Maybe if you spent a minute or two reading what's there on
http://php.net/foreach ,
and trying some code out, you could have solved the problem using far less
effort
than it has taken telling everybody how you don't want to read and learn the
answer
yourself.

Matt
Jul 17 '05 #7
I noticed that Message-ID:
<QG******************@fe3.news.blueyonder.co.uk> from Matt Mitchell
contained the following:
OK, I give up - here's the fish.


LOL...

--
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 #8
Leonardo wrote:
Thaks everybody for all thoses suggestions.

I said that I need to print. But actually I need to process every data
on this associative array. That's why print_r doesn't work for me.

So, here is the associative array again,

$user["john"]["mozilla"]=1;
$user["john"]["xmms"]=1;
$user["doe"]["mozilla"]=0;
$user["doe"]["office"]=1;
$user["paul"]["mozilla"]=0;
$user["paul"]["xmms"]=1;
$user["paul"]["office"]=1;

I need the following output in my foreach statement:

john mozilla 1
john xmms 1
doe mozilla 0
doe office 1
etc...

If I got the above output from my associative array, I will be able to
genarate the statistics I need, processing each element (instead of
just printing them).


A little ugly, but:

<?php
$user["john"]["mozilla"]=1;
$user["john"]["xmms"]=1;
$user["doe"]["mozilla"]=0;
$user["doe"]["office"]=1;
$user["paul"]["mozilla"]=0;
$user["paul"]["xmms"]=1;
$user["paul"]["office"]=1;

$keys = array_keys($user);
for($i = 0; $i < count($keys); $i++) {
$user_keys = array_keys($user[$keys[$i]]);
for($j = 0; $j < count($user_keys); $j++) {
print($keys[$i] . ' ' . $user_keys[$j] . ' ' .
$user[$keys[$i]][$user_keys[$j]]
.. "\n");
}
}
?>

?
Sacs

~
~
Jul 17 '05 #9
> A little ugly, but:

.... it works! Thank you.
Jul 17 '05 #10
JDS
On Wed, 16 Feb 2005 03:03:42 -0800, Leonardo wrote:
A little ugly, but:


... it works! Thank you.


But ugly is bad in the long run. Works now, yes... but how about later,
when you want to change, add, debug, or modify the thing?

Matt Mitchell's example is way better.

--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Jul 17 '05 #11
JDS
On Wed, 16 Feb 2005 00:43:28 +0000, Matt Mitchell wrote:
OK, I give up - here's the fish.


Awww... you gave up too easily.

--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Jul 17 '05 #12

"JDS" <je*****@example.invalid> wrote in message
news:pa****************************@example.invali d...
On Wed, 16 Feb 2005 00:43:28 +0000, Matt Mitchell wrote:
OK, I give up - here's the fish.


Awww... you gave up too easily.


"Give a man a fish, and you feedcx him for a day; teach a man how to fish,
and you feed him for life."

I heard a much much better version the other day:

"Give a man a hot meal, and you'll keep him warm for an hour. Set a man on
fire, and you'll keep him warm for the rest of his life."

Somehow it sprang to mind when I was reading this thread...

Matt
Jul 17 '05 #13
JDS
On Wed, 16 Feb 2005 16:37:58 +0000, Matt Mitchell wrote:
"Give a man a fish, and you feedcx him for a day; teach a man how to fish,
and you feed him for life."


Actually, I got it. Thanks.

<http://groups-beta.google.com/group/alt.php/browse_frm/thread/b199404da0add7bb/fc************************@jhu.edu+O************** @jhu.edu+O*********@go.away.com+O*********@pantsjh u.edu&_****************************@jhu.edu+O***** *********@jhu.edu+O*********@go.away.com+O******** *@pantsjhu.edu%26start%3D0%26scoring%3Dd%26hl%3Den %26ie%3DUTF-8%26oe%3DUTF-8%26safe%3Doff%26&_doneTitle=Back+to+Search&&d#fcc d3327a22daa06>

--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Jul 17 '05 #14

"JDS" <je*****@example.invalid> wrote in message
news:pa****************************@example.invali d...
On Wed, 16 Feb 2005 16:37:58 +0000, Matt Mitchell wrote:
"Give a man a fish, and you feedcx him for a day; teach a man how to
fish,
and you feed him for life."


Actually, I got it. Thanks.


I know you got it - just thought you might find the alternative version
amusing!...

<g>

Matt
Jul 17 '05 #15
With respect then to curiosity, the teacher has usually more to learn
than to teach. Rarely can he aspire to the office of kindling or
even increasing it. His task is rather to keep alive the sacred spark
of wonder and to fan the flame that already glows. His problem is to
protect the spirit of inquiry, to keep it from becoming blasť from
overexcitement, wooden from routine, fossilized through dogmatic
instruction, or dissipated by random exercise upon trivial things.

http://spartan.ac.brocku.ca/~lward/D...ey_1910_c.html

Jul 17 '05 #16
<gu******@yahoo.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
With respect then to curiosity, the teacher has usually more to learn
than to teach. Rarely can he aspire to the office of kindling or
The student has much to learn if he is not prepared to work on the problem
himself
even increasing it. His task is rather to keep alive the sacred spark
of wonder and to fan the flame that already glows. His problem is to
I would imagine that someone posessed of the "scared spark of wonder" would
have been prepared to try *reading* the excellent instructions and examples
that are avaiable free on php.net, to discover the answer he needed
protect the spirit of inquiry, to keep it from becoming blasť from
overexcitement, wooden from routine, fossilized through dogmatic
instruction, or dissipated by random exercise upon trivial things.


Does "dogmatic instruction" include just giving someone the answer, rather
than teaching him how to solve the problem class himself?

Why is it that I was able to answer his question because I learnt how PHP
handles arrays by reading the docs, and yet he is unable to read the same
docs himself, but rather prefers to let other people do the work for him?

Matt

Jul 17 '05 #17
I noticed that Message-ID:
<11*********************@c13g2000cwb.googlegroups. com> from
gu******@yahoo.com contained the following:
With respect then to curiosity, the teacher has usually more to learn
than to teach. Rarely can he aspire to the office of kindling or
even increasing it. His task is rather to keep alive the sacred spark
of wonder and to fan the flame that already glows. His problem is to
protect the spirit of inquiry, to keep it from becoming blasť from
overexcitement, wooden from routine, fossilized through dogmatic
instruction, or dissipated by random exercise upon trivial things.


Bloody hell, I definitely don't get paid enough then...

--
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 #18
Matt Mitchell wrote:

"JDS" <je*****@example.invalid> wrote in message
news:pa****************************@example.invali d...
On Wed, 16 Feb 2005 00:43:28 +0000, Matt Mitchell wrote:
OK, I give up - here's the fish.


Awww... you gave up too easily.


"Give a man a fish, and you feedcx him for a day; teach a man how to fish,
and you feed him for life."

I heard a much much better version the other day:

"Give a man a hot meal, and you'll keep him warm for an hour. Set a man on
fire, and you'll keep him warm for the rest of his life."

Somehow it sprang to mind when I was reading this thread...

Matt


"Give a man a fish, and you feed him for a day. Teach him to fish, and
he'll spend all his money on beer and bait."

--

To reply, delete the 'x' from my email
Jerry Stuckle,
JDS Computer Training Corp.
js*******@attglobal.net
Member of Independent Computer Consultants Association - www.icca.org
Jul 17 '05 #19
You were able to answer out of guilt of having recieved all this free
stuff off the internet. He has not yet felt the burden of having
recieved all the free stuff...

Jul 17 '05 #20
OTOH I would agree some people are definitely useless idiots, sponges
on society, takers, etc. who need to be slapped.

Jul 17 '05 #21
JDS
On Wed, 16 Feb 2005 16:50:41 +0000, Matt Mitchell wrote:
I know you got it - just thought you might find the alternative version
amusing!...

<g>

Matt


Sorry. Yes, actually, I did!

--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Jul 17 '05 #22

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mark | last post: by
2 posts views Thread by paul | last post: by
6 posts views Thread by Ant | last post: by
8 posts views Thread by Derek Basch | last post: by
3 posts views Thread by matthewburton | last post: by
41 posts views Thread by Rene Nyffenegger | last post: by
5 posts views Thread by KDawg44 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.