Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-29-2011, 08:04 AM   PM User | #1
Anishgiri
Regular Coder

 
Join Date: May 2010
Posts: 179
Thanks: 0
Thanked 0 Times in 0 Posts
Anishgiri is an unknown quantity at this point
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.
Anishgiri is offline   Reply With Quote
Old 11-29-2011, 08:18 AM   PM User | #2
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
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
BluePanther is offline   Reply With Quote
Old 11-29-2011, 08:54 AM   PM User | #3
Anishgiri
Regular Coder

 
Join Date: May 2010
Posts: 179
Thanks: 0
Thanked 0 Times in 0 Posts
Anishgiri is an unknown quantity at this point
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++; 
Anishgiri is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:14 AM.


Advertisement
Log in to turn off these ads.