3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = <<<HTML <html> <head> </head> <body> <table> <tbody> <tr> <td>header1</td> <td>header2</td> <td>header3</td> </tr> <tr> <td>content1</td> <td>content2</td> <td>content3</td> </tr> <tr> <td>test1</td> <td>test2</td> <td>test3</td> </tr> </tbody> </table> </body> </html> HTML; $dom = new DOMDocument(); $dom->loadHTML($html); $result = []; foreach ($dom->getElementsByTagName('tr') as $i => $row) { foreach ($row->getElementsByTagName('td') as $c => $cell) { $result[$c][$i] = $cell->nodeValue; } } var_export($result);
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
array ( 0 => array ( 0 => 'header1', 1 => 'content1', 2 => 'test1', ), 1 => array ( 0 => 'header2', 1 => 'content2', 2 => 'test2', ), 2 => array ( 0 => 'header3', 1 => 'content3', 2 => 'test3', ), )

preferences:
57.49 ms | 1152 KiB | 4 Q