How to print a multidimensional associative array?

This is a discussion on How to print a multidimensional associative array? within the PHP Language forums, part of the PHP Programming Forums category; I have the following associative array: $user["john"]["mozilla"]=1; $user["john"]["xmms&...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-15-2005
Leonardo
 
Posts: n/a
Default 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??
Reply With Quote
  #2 (permalink)  
Old 02-15-2005
JDS
 
Posts: n/a
Default Re: How to print a multidimensional associative array?

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 | jeffrey@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Reply With Quote
  #3 (permalink)  
Old 02-15-2005
NC
 
Posts: n/a
Default Re: How to print a multidimensional associative array?

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

Reply With Quote
  #4 (permalink)  
Old 02-15-2005
rush
 
Posts: n/a
Default Re: How to print a multidimensional associative array?

"Leonardo" <leonardomachado@gmail.com> wrote in message
news:a3c20c30.0502150940.5947a35@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/


Reply With Quote
  #5 (permalink)  
Old 02-16-2005
Leonardo
 
Posts: n/a
Default Re: How to print a multidimensional associative array?

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).
Reply With Quote
  #6 (permalink)  
Old 02-16-2005
Anonymous
 
Posts: n/a
Default Re: How to print a multidimensional associative array?

"Leonardo" <leonardomachado@gmail.com> wrote in message
news:a3c20c30.0502151537.5d2e19b@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


Reply With Quote
  #7 (permalink)  
Old 02-16-2005
Matt Mitchell
 
Posts: n/a
Default Re: How to print a multidimensional associative array?


"Leonardo" <leonardomachado@gmail.com> wrote in message
news:a3c20c30.0502151537.5d2e19b@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


Reply With Quote
  #8 (permalink)  
Old 02-16-2005
Geoff Berrow
 
Posts: n/a
Default Re: How to print a multidimensional associative array?

I noticed that Message-ID:
<QGwQd.98976$B8.69881@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/
Reply With Quote
  #9 (permalink)  
Old 02-16-2005
Sacs
 
Posts: n/a
Default Re: How to print a multidimensional associative array?

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

~
~
Reply With Quote
  #10 (permalink)  
Old 02-16-2005
Leonardo
 
Posts: n/a
Default Re: How to print a multidimensional associative array?

> A little ugly, but:

.... it works! Thank you.
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 10:15 AM.


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