Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can i add missing values of a multidimensional associative array to an HTML table using PHP
    primarykey
    data
    text
    <p>I'm attempting to print an array to html table in php and am having issues where the array elements differ in number (eg. missing fields). </p> <p>How can I solve the problem where sometimes one (or more) header elements are missing from the array, so the values end up under the wrong header?</p> <p>Here's my code. I think I need to add another loop to make sure all of the $rows line up with the $keys...?</p> <p>Input Array:</p> <pre><code>array (size=16) 0=&gt; 'created_by' =&gt; string 'me@example.com' (length=31) 'bug_status' =&gt; string 'verified' (length=8) 'reported_by' =&gt; string 'me@example.com' (length=31) 'modified_ts' =&gt; string '1413503800000' (length=13) 'bug_id' =&gt; string '123' (length=3) 'bug_severity' =&gt; string 'normal' (length=6) 'product' =&gt; string 'core graveyard' (length=14) 'bug_version_num' =&gt; string '9' (length=1) 'assigned_to' =&gt; string 'me@example.com' (length=19) 'op_sys' =&gt; string 'windows nt' (length=10) '_id' =&gt; string '123.1217503800000' (length=17) 'component' =&gt; string 'viewer app' (length=10) 'modified_by' =&gt; string 'nobody@example.org' (length=18) 'priority' =&gt; string 'p2' (length=2) 'qa_contact' =&gt; string '#unknown' (length=8) 'created_ts' =&gt; string '901720800000' (length=12) </code></pre> <p>Here's my PHP code:</p> <pre><code>$keys = array_keys($array[0]); echo "&lt;table&gt;&lt;tr&gt;&lt;th&gt;".implode("&lt;/th&gt;&lt;th&gt;", $keys)."&lt;/th&gt;&lt;/tr&gt;"; foreach ($array as $rows) { if (!is_array($rows)) continue; echo "&lt;tr&gt;&lt;td&gt;".implode("&lt;/td&gt;&lt;td&gt;", $rows )."&lt;/td&gt;&lt;/tr&gt;"; } echo "&lt;/table&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload