简体   繁体   English

混淆php中的多维数组与foreach获取错误

[英]confusing multidimensional array in php with foreach getting error

what is use of multidimensional array(2D,3D or what is the limit in multidimensional array) and foreach()?多维数组(2D、3D 或多维数组的限制是什么)和 foreach() 的用途是什么? foreach() is use for printing values inside array? foreach() 用于打印数组内的值? 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) }注意: C:\\xampp\\htdocs\\example\\basic\\foreach2.php 中第 9 行的数组到字符串的转换Arrayarray(3) { [0]=> int(4) [1]=> int(5) [2] => 整数(7)}

Notice: Array to string conversion in C:\\xampp\\htdocs\\example\\basic\\foreach2.php on line 11注意: C:\\xampp\\htdocs\\example\\basic\\foreach2.php 中第 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??????错误 1. 注意:数组到字符串的转换 2. 未定义的索引:$android_services 3. 如何在 foreach 的帮助下打印 4. 上面的数组是 4 维的??????

To loop through this kind of array you need some sort of recursiveness.要遍历这种数组,您需要某种递归。

I usually call a function inside the for each.我通常在 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;   
}

You can define multidimension arrays (arrays including oher arrays) in PHP.您可以在 PHP 中定义多维数组(数组包括其他数组)。

For example you have 2 different lists, one for grocery shopping, one for daily task.例如,您有 2 个不同的列表,一个用于购物,一个用于日常任务。

$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 ,它将迭代第一级数组。

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.但是,如果您知道结构,则可以直接使用$lists['grocery']来访问杂货数组或使用$lists['grocery'][0]来访问杂货清单的第一个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM