Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

what is use of multidimensional array(2D,3D or what is the limit in multidimensional array) and foreach()? foreach() is use for printing values inside array? In case of multidimensional array why nested loop is important?

Errors:

Notice: Array to string conversion in C:\xampp\htdocs\example\basic\foreach2.php on line 9 Arrayarray(3) { [0]=> int(4) [1]=> int(5) [2]=> int(7) }

Notice: Array to string conversion in C:\xampp\htdocs\example\basic\foreach2.php on line 11

$items = array(1,2,3,
      array(4,5,7
     ),
     8,54,4,5,5);
foreach($items as $key => $value)
{
   echo $value;
   var_dump($value);
   echo $key ."pair match".$value . "<br>";
}

HOW do I access this array?

      $a_services = array(
        'user-login' => array(
          'operations' => array(
            'retrieve' => array(
              'help' => 'Retrieves a user',
              'callback' => 'android',
              'file' => array('type' => 'inc', 'module' => 'android_services'),
              'access callback' => 'services',
              'args' => array(
                array(
                  'name' => 'phone_no',
                  'type' => 'string',
                  'description' => 'The uid ',
                  'source' => array('path' => 0),
                  'optional' => FALSE,
                ),
              ),
            ),
          ),
         ),
        );
  print_r($a_services['$android_services ']['user-login']['operations']['retrieve']['callback']);
 print_r($a_services['$android_services ']['user-login']['operations']['retrieve']['callback']['args']['name']);

Error 1. Notice: Array to string conversion 2. Undefined index: $android_services 3. How to print with help of foreach 4. above array is 4 dimensional??????

share|improve this question
    
    
There are millions of examples for accessing nested array values alone here on SO. None of that helped to understand? _Why not? – arkascha Oct 10 '15 at 18:16

To loop through this kind of array you need some sort of recursiveness.

I usually call a function inside the for each. the function tests whether the current element is an array. If so the functions call itself. If not the function does whatever (echo the value in your example).

Something like:

foreach($a_services as $key => $value) {
    do_your_thing($value);
}

function do_your_thing($recvalue)
{
    if (is_array($recvalue))
    {
        foreach($recvalue as $key => $value) 
        {
            do_your_thing($value);
        }
    }
    else
    {
        echo $recvalue;
    }
    return $recvalue;   
}
share|improve this answer

You can define multidimension arrays (arrays including oher arrays) in PHP.

For example you have 2 different lists, one for grocery shopping, one for daily task.

$lists = array(
    'grocery' => array('banana', 'apple'),
    'tasks' => array('go to work', 'wash dishes'),
);

If you want to check all levels of arrays, you should use nested loops. For example you can use foreach, it will iterate over arrays first level.

foreach($lists as $list)
    return $list; // returns grocery[] and tasks[]

As you see this loop returning other loop. So if you need to list all grocery[] array items, you need iterate again in this array.

foreach($lists as $list)
    foreach($list as $k => $l)
        if($k == 'grocery')
            echo $l; // echos "banana","apple"

Nested loops are important (and necessary) to reach multidimensional arrays contents especially if you don't know structure of the array. These loops will find all array items for you. But if you know structure, you can directly use $lists['grocery'] to reach grocery array or $lists['grocery'][0] to reach first element of your grocery list.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.