0

I have multidimensional associative array as follows

  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

and so on....

now i want print them in table format I have used following code for output

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>";
        }

Please help me to get out as

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

and so on

I am getting error Invalid argument supplied for foreach()

Thanks in advance

2 Answers2

0

The fundamental issue here is that when you are iterating over sr_array, you are assuming that the values are also arrays. This is not the case for index 0 through 8 (from what I can tell). This would explain the immediate issue.

Ideally, the structure of your array is consistent (ideally, a two-dimensional array of rows, each containing a set of fields), then a simple nested loop will work.

0

Try the below solution with a combination of for()/foreach() and looped iteration.

Also, consider a transposed html table with data as the columns and descriptives as rows. To do otherwise requires complex iteration as the array items are not aligned to the html markup.

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";

And output as below (I repeat your posted data for example):

               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
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • Sorry taking so much time to reply. I run your code its showing few errors please guide me. First foreach($sr_array as $item) { echo "". array_keys($item)[$i] ."\n"; $i++; } When I run above code loop runs 18 time were i have 9 items in my array. Second its shows error in foreach($item as $key => $value) { echo "". $value . "\n"; } – priyesh kanodiya Aug 19 '15 at 00:59
  • Our `sr_array` may be defined differently. I used named keys: `$sr_array['Shop']['count_auto'] = 60 ...` Please edit your post in how you define the array (just a few first items). – Parfait Aug 20 '15 at 22:27