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&...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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?? |
|
|||
|
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/ |
|
|||
|
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 |
|
|||
|
"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/ |
|
|||
|
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). |
|
|||
|
"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 |
|
|||
|
"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 |
|
|||
|
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/ |
|
|||
|
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 ~ ~ |