4

My associate array is as follows:

   <?php
     $marks = array( 
        "mohammad" => array (
           "physics" => 35,
           "maths" => 30,   
           "chemistry" => 39
        ),

        "qadir" => array (
           "physics" => 30,
           "maths" => 32,
           "chemistry" => 29
        ),

        "zara" => array (
           "physics" => 31,
           "maths" => 22,
           "chemistry" => 39
        )
     );      

     ?>

expected output as follows in table format using for loop:

<table border="1">
        <tr><td>Name </td><td>  physics</td><td> maths </td><td>chemistry</td></tr>
     <tr><td>mohammad</td><td>  35    </td><td>   30</td><td>        39</td></tr>
     <tr><td>qadir   </td><td>   30 </td><td>     32</td><td>        29</td></tr>
          <tr><td>zara   </td><td>   31   </td><td>   22     </td><td>    39</td></tr>
</table>

enter image description here

Any help will be appreciated. Thanks.

4
  • Any help will be appreciated. thanks in advance Commented May 15, 2017 at 6:28
  • 1
    What have you tried with the for() loop since you expect the output in table format with this function? Commented May 15, 2017 at 6:29
  • @C0dekid he need for loop code Commented May 15, 2017 at 6:30
  • Actually, I need output as shown in table: <table border=1 width=auto> <thead> <tr> <th>Name</th><th>physics</th><th>maths</th><th>chemistry</th> </tr> </thead> <tbody> <tr> <td>mohammad</td><td>35</td><td>30</td><td>39</td> </tr> <tr> <td>qadir</td><td>30</td><td>32</td><td>29</td> </tr> <tr> <td>zara</td><td>31</td><td>22</td><td>39</td> </tr> </tbody> </table> Commented May 15, 2017 at 6:31

4 Answers 4

3

Use foreach() on two levels, one for name and another one for marks with the table tags as string embedded with php foreach() loop.

Ex:

foreach($marks as $name => $mark)
{
    echo "<tr><td>".$name."</td>";
    foreach($mark as $key => $value)
    {
        echo "<td>".$value."</td>";
    }
    echo "</tr>";
}
Sign up to request clarification or add additional context in comments.

Comments

1

Tested and it works How could you expecting: Use foreach inside foreach.

echo ' <table border=1 width=auto> <thead> <tr> <th>Name</th><th>physics</th><th>maths</th><th>chemistry</th‌​> </tr> </thead>';
echo '<tbody>  ';
foreach($marks as $key => $value)
{
   echo "<tr> <td>".$key."</td>";
   foreach($value as $strin)
   {
       echo '<td>'.$strin.'</td>';
   }
   echo '</tr> ';
  }
  echo '</tbody> </table>';

1 Comment

Yes, but what my aim was that, using foreach or for loop should read that array dynamically, person names, subject names and marks are all should display automatically. but i did not know that how to handle it, but still your approach is very much helpful. thanks a lot.
0

Try this:

foreach ($marks as $key => $value) {
 echo $key;
      foreach ($marks as $key => $value) {
       echo $value;

    }
}

Comments

0

use foreach like

<th>
  <td>Name</td>
 <td>Physics</td>
 <td>math</td>
 <td>chemistry</td>
foreach($data as $array)
{
   echo "<tr><td>".$array['name']."</td>
           <td>".$array['physics']".</td>
         <td>".$array['math']".</td>
         <td>".$array['chemistry']".</td>
     </tr>";
  }

2 Comments

actually, first column will be name, second column will be physics marks, third column will have maths marks, and fourth column will have a chemistry marks dynamically without writing header names. is it possible using for loop dynamically?, thanks in advance.
I think there's no array index like $array['name'].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.