【问题标题】:php foreach multidimensional associative array [duplicate]php foreach多维关联数组[重复]
【发布时间】:2015-11-09 14:25:48
【问题描述】:

我有如下多维关联数组

  sr_array=>
    array (size=18)
  0 => string 'Shop' (length=4)
  1 => string 'Papu Kushwaha' (length=13)
  2 => string 'Jitendra Shukla' (length=15)
  3 => string 'Satish' (length=6)
  4 => string 'Pradeep' (length=7)
  5 => string 'Papu Khan' (length=9)
  6 => string 'Bharat' (length=6)
  7 => string 'Manoj Pandey' (length=12)
  8 => string 'select' (length=6)
  'Shop' => 
    array (size=9)
      'count_auto' => int 60
      'count_indus' => int 4
      'count_mc' => int 100
      'count_inverter' => int 10
      'count_in_bat' => int 40
      'total_credit' => int 191850
      'sale_value' => int 1351755
      'total_disc' => float 22377.38
      'perc_disc' => float 1.66

等等……

现在我想以表格格式打印它们,我使用以下代码进行输出

echo"<table border=1>";
            echo"<tr>";
            echo"<th></th>";


        for($i=0; $i < count($sr_array)/2; $i++){
            $current_sr = $sr_array[$i];
            echo"<th>{$current_sr}</th>";
        }
        echo"</tr>";

        $keys = array_keys($sr_array);

        for($i=0; $i < count($sr_array)/2; $i++){
            $current_sr = $sr_array[$i];
            echo"<tr>";
            echo"<td></td>";

            foreach($sr_array as $key => $var){
                 foreach($var as $x => $y)
                    echo"<td>{$y["count_auto"]}</td>";
             }

            echo"</tr>";
        }

请帮我出去

-------------------------------------
|           |Shop    |xyz      |abc   |
-------------------------------------
count_auto  |60      |75       | 85   |
-------------------------------------
count_indus | 25     | 74      |15    |
--------------------------------------
count_mc    |  55    | 212     | 15   |
-------------------------------------

等等

我收到错误为 foreach() 提供的参数无效

提前致谢

【问题讨论】:

    标签: php loops multidimensional-array foreach associative-array


    【解决方案1】:

    这里的基本问题是,当您迭代 sr_array 时,您假设值也是数组。索引 0 到 8 的情况并非如此(据我所知)。这将解释当前的问题。

    理想情况下,您的数组结构是一致的(理想情况下,二维数组行,每个包含一组字段),然后一个简单的嵌套循环将起作用。

    【讨论】:

      【解决方案2】:

      结合for()/foreach() 和循环迭代尝试以下解决方案。

      另外,考虑一个转置的 html 表,其中数据作为列,描述性作为行。否则需要复杂的迭代,因为数组项未与 html 标记对齐。

      echo "\n<table>\n";
      
      $i = 0;
      echo "\n<tr>\n";
      echo "\n<th></th>\n";
      foreach($sr_array as $item) {                 
          echo "<th>". array_keys($item)[$i] ."</th>\n";
          $i++;
      }
      echo "</tr>\n";
      
      $j=0;
      foreach($sr_array as $item) {
      
          echo "\n<tr>\n";
          echo "<td>" . array_keys($sr_array)[$j] . "</td>\n";
                  foreach($item as $key => $value)
                  {
                      echo "<td>". $value . "</td>\n";
                  }
          echo "</tr>\n";
          $j++;
      }
      echo "\n</table>\n";
      

      并输出如下(例如,我重复您发布的数据):

                     count_auto   count_indus count_mc count_inverter     count_in_bat total_credit sale_value total_disc perc_disc
      Shop            60          4   100     10  40  191850  1351755     22377.38    1.66
      Papu Kushwaha   60          4   100     10  40  191850  1351755     22377.38    1.66
      Jitendra Shukla 60          4   100     10  40  191850  1351755     22377.38    1.66
      Satish          60          4   100     10  40  191850  1351755     22377.38    1.66
      Pradeep         60          4   100     10  40  191850  1351755     22377.38    1.66
      Papu Khan       60          4   100     10  40  191850  1351755     22377.38    1.66
      Bharat          60          4   100     10  40  191850  1351755     22377.38    1.66
      Manoj Pandey    60          4   100     10  40  191850  1351755     22377.38    1.66
      select          60          4   100     10  40  191850  1351755     22377.38    1.66
      

      【讨论】:

      • 抱歉花了这么多时间回复。我运行您的代码,它显示了一些错误,请指导我。首先 foreach($sr_array as $item) { echo "". array_keys($item)[$i] ."\n"; $i++;当我在上面运行代码循环运行 18 次时,我的数组中有 9 个项目。其次,它在 foreach($item as $key => $value) { echo "" 中显示错误。 $价值。 "\n"; }
      • 我们的sr_array 的定义可能不同。我使用了命名键:$sr_array['Shop']['count_auto'] = 60 ... 请根据您定义数组的方式编辑您的帖子(只是一些第一项)。
      猜你喜欢
      • 2021-12-13
      • 2018-07-05
      • 2018-03-01
      • 2013-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多