|
-
May 30th, 2009, 03:21 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Multi-Dimensional Arrays
I'm basically trying to cycle though the first dimension of a multi-dimensional array. Please look at the code below:
PHP Code:
<?php
$clientList['client1']['IP']="192.168.1.1";
$clientList['client1']['Name']="Client 1";
$clientList['client1']['URI']="client-1";
$clientList['client2']['IP']="192.168.1.2";
$clientList['client2']['Name']="Client 2";
$clientList['client2']['URI']="client-2";
$clientList['client3']['IP']="192.168.1.3";
$clientList['client3']['Name']="Client 3";
$clientList['client3']['URI']="client-3";
foreach ($clientList as $clientListIndex)
{
echo $clientList[$clientListIndex]['Name'];
}
?>
I'm not sure how the syntax of this should be.
-
May 30th, 2009, 03:24 AM
#2
Thread Starter
Fanatic Member
Re: Multi-Dimensional Arrays
Figured it out, but not sure why it needs the "=> $clientListItem".
PHP Code:
<?php
$clientList['client1']['IP']="192.168.1.1";
$clientList['client1']['Name']="Client 1";
$clientList['client1']['URI']="client-1";
$clientList['client2']['IP']="192.168.1.2";
$clientList['client2']['Name']="Client 2";
$clientList['client2']['URI']="client-2";
$clientList['client3']['IP']="192.168.1.3";
$clientList['client3']['Name']="Client 3";
$clientList['client3']['URI']="client-3";
foreach ($clientList as $clientListIndex => $clientListItem)
{
echo $clientList[$clientListIndex]['Name'];
}
?>
-
May 30th, 2009, 01:44 PM
#3
Re: [RESOLVED] Multi-Dimensional Arrays
because you're using a foreach loop, you can actually echo "name" with just $clientListItem['name']. the foreach construct works like this:
Code:
foreach($array as $value)
OR
foreach($array as $key => $value)
in your case, if you only defined $value, then $value would always be an array, and that wouldn't really help you at all! if you defined a key and value, then $key will be equal to
"client1" on the first iteration and $value will be equal to the 3 element array that you have for client1 (IP, Name, URI). hope that makes sense!
-
May 31st, 2009, 01:08 AM
#4
Thread Starter
Fanatic Member
Re: [RESOLVED] Multi-Dimensional Arrays
It does make sense since you explained it that way. I got confused with the => thing!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|