Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## 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.

• 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  ```

• 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
•