Results 1 to 4 of 4

Thread: [RESOLVED] Multi-Dimensional Arrays

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Resolved [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.

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    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'];
    }

    ?>

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    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!

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    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
  •  



Click Here to Expand Forum to Full Width