Note that there are some explanatory texts on larger screens.

plurals
  1. POMissing PHP array grandchild
    text
    copied!<p>I have some code for building a HTML table from PHP.</p> <pre><code>&lt;?php function new_table($class) { $table = array('class' =&gt; $class, 'tr' =&gt; array()); return $table; } function new_tr(&amp;$table, $class) { $tr = array('class' =&gt; $class, 'td' =&gt; array()); $table['tr'][] = $tr; return $tr; } function new_td(&amp;$tr, $class, $content) { $td = array('class' =&gt; $class, 'content' =&gt; $content); $tr['td'][] = $td; return $td; } ?&gt; </code></pre> <p>Usage:</p> <pre><code> $table = new_table('admin whoami'); $tr = new_tr($table, 'head'); new_td($tr, 'field', 'Field'); new_td($tr, 'value', 'Value'); $tr = new_tr($table, 'body'); new_td($tr, 'field', 'Name'); new_td($tr, 'value', $my_name); echo '--- tr ---'.chr(10); var_dump($tr); echo '--- table ---'.chr(10); var_dump($table); </code></pre> <p>And the result:</p> <pre><code>--- tr --- array(2) { ["class"]=&gt; string(4) "body" ["td"]=&gt; array(2) { [0]=&gt; array(2) { ["class"]=&gt; string(5) "field" ["content"]=&gt; string(4) "Name" } [1]=&gt; array(2) { ["class"]=&gt; string(5) "value" ["content"]=&gt; string(5) "user0" } } } --- table --- array(2) { ["class"]=&gt; string(12) "admin whoami" ["tr"]=&gt; array(2) { [0]=&gt; array(2) { ["class"]=&gt; string(4) "head" ["td"]=&gt; array(0) { } } [1]=&gt; array(2) { ["class"]=&gt; string(4) "body" ["td"]=&gt; array(0) { } } } } </code></pre> <p>If you note, var_dump($tr) correctly dumps the tr, including child td elements.</p> <p>However, var_dump($table), while correctly var_dumping the tr elements, does not var_dump the grandchild td elements -- note the empty array ["td"]=> array(0) { }</p> <p>Do you know why this might be happening?</p> <p>Does it have something to do with the returned $tr element being a copy, not a reference, of a $table['tr'] element? In which case, why does adding a tr to the table work, when adding a td to a tr doesn't?</p> <p>Thanks!</p>
 

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