【问题标题】:How to loop through the following php array?如何循环遍历下面的 php 数组?
【发布时间】:2016-12-01 09:59:20
【问题描述】:

我想遍历下面的数组。

array(2)  {
    [0]=> array(6) {
        [0]=> string(10) "excel_id "
        [1]=> string(12) "excel_name "
        [2]=> string(11) "excel_email"
        [3]=> string(13) "excel_address"
        [4]=> string(12) "excel_giveby"
        [5]=> string(16) "excel_accesories" 
    } 
    [1]=> array(6) {
        [0]=> float(111)
        [1]=> string(10) "sh1exname1"
        [2]=> string(11) "sh1exemail1"
        [3]=> string(12) "sh1exaddress"
        [4]=> string(12) "sh1exgivenby"
        [5]=> string(16) "sh1exaccesorries" 
    }    
}

谁能帮帮我?

【问题讨论】:

  • 尝试在开始时使用foreach循环
  • 提问前请查看这里:stackoverflow.com/help/how-to-ask
  • 您是否尝试过查看有关数组的 PHP 手册?
  • 这与在任何其他编程语言中遍历大小为 2X6 的二维数组相同。使用两个循环并遍历整个数组。
  • foreach ($data as $innerArray) { if (is_array($innerArray)){ // 扫描内循环 foreach ($innerArray as $value) { var_dump($value); } }else{ 回声 $innerArray; } }

标签: php multidimensional-array


【解决方案1】:

如果你尝试过任何东西,你应该已经找到了 foreach。 这将遍历所有数组值。 您可以使用这样的多维 foreach 简单地遍历多维数组:

foreach($array as $parentValue)
{
    foreach($parentValue as $childValue)
    {
        // your code
    }
}

【讨论】:

    【解决方案2】:

    试试这个:

    foreach ($main_array as $outer_entry){
    
        foreach($outer_entry as $entity){
            print_r($entity);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2011-05-23
      • 2012-11-14
      • 2011-04-25
      • 1970-01-01
      • 2019-07-10
      • 1970-01-01
      • 2011-06-11
      • 2013-02-06
      • 2011-10-30
      相关资源
      最近更新 更多