473,503 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 18840
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
14020
by: Mark | last post by:
Does Smarty support assigning multidimensional arrays? I've tried to get it working, but just can't get it. can someone help me? this is what I have in my php file: $side_bar = array('link'...
5
6737
by: Golf Nut | last post by:
I am finding that altering and affecting values in elements in multidimensional arrays is a huge pain in the ass. I cannot seem to find a consistent way to assign values to arrays. Foreach would...
3
2129
by: jdemello | last post by:
When using a multidimensional associative array to cross-reference data in a second, single-dimensional array, PHP stops processing data after a few iterations. Memory usage according to the task...
2
8519
by: paul | last post by:
Hi all, I have a 2D array, which I am trying to access, but I am having problems doing this: If, for instance, I use a nested for loop to go through the values, using something like echo...
6
1890
by: Ant | last post by:
Hi, I'm trying to store some user input from a web form into an array, now the thing is I would like to store multiple users entries so I was thinking of using a multidimensional array so I...
8
7673
by: Derek Basch | last post by:
Is there any way to associate name/value pairs during an array initialization? Like so: sType = "funFilter" filterTypeInfo = ; filterTypeInfo = new Array("type" : sType); I can do it using...
3
1859
by: matthewburton | last post by:
Hi everyone, I'm trying to write a program that will search a body of text and replace certain words (say, A, B, and C) with different words that I think are more appropriate (A1, B1 and C1). I...
41
4874
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
5
5371
by: KDawg44 | last post by:
Hi, Is there a way to get a multidimensional associative array with the entire result set? I would like to get a an array like this: resultsArray How can I accomplish this? Can I do...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7011
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
401
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.