3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $html = '<html> <body> <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0> <TR> <TD BGCOLOR=000000> <TABLE BORDER=0 CELLSPACING=1 CELLPADDING=4> <TR STYLE="font-weight: 700;" ALIGN=CENTER BGCOLOR=99CCFF> <td>Product</td> <td>Quantity</td> <td>Retail Price</td> <td>Total Retail Price</td> <td>UPC</td> </tr> <tr bgcolor="FFFFFF"> <td style="">Smith High-Water Chroma Pop+ Polar Chromic NXT Sunglasses, Black</td> <td style="text-align: right;">1</td> <td style="text-align: right;">$259.00</td> <td style="text-align: right;">$259.00</td> <td style=""></td> </tr> <tr bgcolor="FFFFFF"> <td style="">Guest 2613A Charge Pro Series Marine Battery Charger (12/24-Volt, 15-Amps 5/5/5, Triple Output)</td> <td style="text-align: right;">1</td> <td style="text-align: right;">$179.18</td> <td style="text-align: right;">$179.18</td> <td style=""></td> </tr> </table> </body> </html>'; $dom = new DOMDocument(); $dom->loadHTML($html); // Grab first table from dom and store its rows and columns by tag name. $rows = $dom->getElementsByTagName('table')->item(1)->getElementsByTagName('tr'); $headers = $rows->item(0)->getElementsByTagName('td'); $resultArray = []; $keys = []; foreach($headers as $header) { $keys[] = strtolower($header->nodeValue); } print_r($keys); foreach($rows as $row) { $item = []; foreach($keys as $i=>$key) { $cells = $row->getElementsByTagName('td'); $item[$key] = $cells->item($i)->nodeValue; } $resultArray[] = $item; } print_r($resultArray);
based on sbk1W
Output for 5.6.38 - 7.3.0rc3
Array ( [0] => product [1] => quantity [2] => retail price [3] => total retail price [4] => upc ) Array ( [0] => Array ( [product] => Product [quantity] => Quantity [retail price] => Retail Price [total retail price] => Total Retail Price [upc] => UPC ) [1] => Array ( [product] => Smith High-Water Chroma Pop+ Polar Chromic NXT Sunglasses, Black [quantity] => 1 [retail price] => $259.00 [total retail price] => $259.00 [upc] => ) [2] => Array ( [product] => Guest 2613A Charge Pro Series Marine Battery Charger (12/24-Volt, 15-Amps 5/5/5, Triple Output) [quantity] => 1 [retail price] => $179.18 [total retail price] => $179.18 [upc] => ) )