簡體   English   中英

php foreach多維關聯數組

[英]php foreach multidimensional associative array

我有如下的多維關聯數組

  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()提供的無效參數

提前致謝

這里的根本問題是,當您遍歷sr_array時,您假設這些值也是數組。 索引0到8並非如此(據我所知)。 這將解釋當前的問題。

理想情況下,數組的結構是一致的(理想情況下,是一個二維的行數組,每個行包含一組字段),然后一個簡單的嵌套循環就可以了。

結合使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM