0

I want to print json_encode array into a table format inside html.I tried the following method:

<table align="center">
   <tr>
        <td style="min-width: 400px; height: 400px; margin: 0 auto;margin-top:50px">
            <div id="container2" style="min-width: 400px; height: 400px; margin: 0 auto;margin-top:50px"></div>       
        </td>
        <td><?php echo json_encode($names1,JSON_NUMERIC_CHECK); ?></td>
    </tr>
</table>

but sadly its just giving the output in normal json_encode. How to print the json_encode output as a table?

10
  • Walk through each member using foreach and output it with proper <tr> and <td> tags. Commented Mar 2, 2013 at 12:50
  • see you cant use foreach loop with json_encode na? Commented Mar 2, 2013 at 12:56
  • Oh, it's ENcode. Why do you want to use json_encode() in the first place? What is your desired output? Commented Mar 2, 2013 at 12:58
  • just to output the value got from mysql, to html near the graph Commented Mar 2, 2013 at 12:58
  • Why do you need json_encode() for that? Why not just output a normal table? Commented Mar 2, 2013 at 12:59

2 Answers 2

1

This is not clear to me, but it sounds like you just want to display an array in HTML table format so I'm not sure what json_encode has to do with it.

However, this has been answered before:

How to create a HTML Table from a PHP array?

Sign up to request clarification or add additional context in comments.

3 Comments

no i want to output the json_encode value into tables. simply echoing it will give the output in the following way [["Ahmed",6],["Aldein",3],["Allouh",2],["Salim",0]]
itried foreach but its giving me array to string conversion error
dued ur awesome..it was already given na ididn notice sorry... :)
0

If you want each value in it's own td element, then you will need to iterate over the array and output the td elements yourself. You can do this in php from the $names array, or in JavaScript on the client using a json array. I'd do it in php.

To output each pair in its own row

foreach ($names1 as &$value) {
Echo "<tr>"
Echo "<td>"
Echo  $value[0]
Echo "</td>
Echo "<td>"
Echo  $value[1]
Echo "</td>
Echo "<tr>
}

3 Comments

ididn understand it properly..how ? imean how to get the json_encode element individually?
but its a two dimensional array. Its giving array to sting conversion error
see these are the values in the array...how to print them? [["Ahmed",6],["Aldein",3],["Allouh",2],["Salim",0]]

Your Answer

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