How can the issue of the extra empty <td></td> be resolved in the PHP code provided?

The issue of the extra empty <td></td> can be resolved by checking if the data exists before adding the <td></td> tags in the loop. This way, only non-empty data will be wrapped in <td></td> tags, preventing the extra empty cells from appearing in the table.

&lt;?php
$data = array(&quot;John&quot;, &quot;Doe&quot;, &quot;30&quot;, &quot;New York&quot;, &quot;&quot;);

echo &quot;&lt;table&gt;&quot;;
foreach ($data as $value) {
    if ($value != &quot;&quot;) {
        echo &quot;&lt;td&gt;$value&lt;/td&gt;&quot;;
    }
}
echo &quot;&lt;/table&gt;&quot;;
?&gt;