Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    May 2010
    Posts
    222
    Thanks
    0
    Thanked 0 Times in 0 Posts

    multidimensional array question

    Suppose, I have a multidimensional array like the one below.

    $value['0']['0'] = "red";
    $value['0']['1'] = "blue";
    $value['0']['2'] = "green";
    $value['0']['3'] = "orange";

    $value['1']['0'] = "dog";
    $value['1']['1'] = "cat";
    $value['1']['2'] = "rabbit";
    $value['1']['3'] = "lion";

    How can I echo the value of an array based on a key? Let say I want to echo the value of the array with the key 0. I can't figure it out. Thanks.

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Multidimensional arrays are arrays within arrays. In other words, a 1D array could be $array[0] = 'string', whereas a 2D array is $array[0] = array(values).
    So, in your scenario (I would remove the ' around the integers, as integer keys should be treated as integers, not strings), $value[0] is an array of colours, and $value[1] is an array of animals. You can see this better by doing this:
    PHP Code:
    $value[0][0] = "red";
    $value[0][1] = "blue";
    $value[0][2] = "green";
    $value[0][3] = "orange";

    $value[1][0] = "dog";
    $value[1][1] = "cat";
    $value[1][2] = "rabbit";
    $value[1][3] = "lion";

    var_dump($value);
    var_dump($value[0]);
    var_dump($value[1]); 
    Now, to print a specific string inside your array, you would do this:
    PHP Code:
    echo $value[0][2]; // echo's green
    echo $value[1][2]; // echo's rabbit 
    http://en.wikipedia.org/wiki/Array_d...nsional_arrays for more information

  • #3
    Regular Coder
    Join Date
    May 2010
    Posts
    222
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the answer(I think my question is not clear), but I figure something, which is seems working on my code. I will just ask later, if there are issues that I encountered.

    PHP Code:
    foreach ($text as $id => $sub
     {
        foreach (
    $sub as $sub2
         {
          if(
    $id==$inc8)
           {
             echo 
    $sub2;
           }
         
         }
       
     }
    $inc8++; 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •