3v4l.org

run code in 300+ PHP versions simultaneously
<?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($cell->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);
Output for 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Warning: Undefined variable $cell in /in/sbk1W on line 44 Warning: Attempt to read property "nodeValue" on null in /in/sbk1W on line 44 Deprecated: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in /in/sbk1W on line 44 Warning: Undefined variable $cell in /in/sbk1W on line 44 Warning: Attempt to read property "nodeValue" on null in /in/sbk1W on line 44 Deprecated: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in /in/sbk1W on line 44 Warning: Undefined variable $cell in /in/sbk1W on line 44 Warning: Attempt to read property "nodeValue" on null in /in/sbk1W on line 44 Deprecated: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in /in/sbk1W on line 44 Warning: Undefined variable $cell in /in/sbk1W on line 44 Warning: Attempt to read property "nodeValue" on null in /in/sbk1W on line 44 Deprecated: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in /in/sbk1W on line 44 Warning: Undefined variable $cell in /in/sbk1W on line 44 Warning: Attempt to read property "nodeValue" on null in /in/sbk1W on line 44 Deprecated: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in /in/sbk1W on line 44 Array ( [0] => [1] => [2] => [3] => [4] => ) Array ( [0] => Array ( [] => UPC ) [1] => Array ( [] => ) [2] => Array ( [] => ) )
Output for 8.0.0 - 8.0.30
Warning: Undefined variable $cell in /in/sbk1W on line 44 Warning: Attempt to read property "nodeValue" on null in /in/sbk1W on line 44 Warning: Undefined variable $cell in /in/sbk1W on line 44 Warning: Attempt to read property "nodeValue" on null in /in/sbk1W on line 44 Warning: Undefined variable $cell in /in/sbk1W on line 44 Warning: Attempt to read property "nodeValue" on null in /in/sbk1W on line 44 Warning: Undefined variable $cell in /in/sbk1W on line 44 Warning: Attempt to read property "nodeValue" on null in /in/sbk1W on line 44 Warning: Undefined variable $cell in /in/sbk1W on line 44 Warning: Attempt to read property "nodeValue" on null in /in/sbk1W on line 44 Array ( [0] => [1] => [2] => [3] => [4] => ) Array ( [0] => Array ( [] => UPC ) [1] => Array ( [] => ) [2] => Array ( [] => ) )
Output for 7.2.0 - 7.2.33, 7.3.16 - 7.3.31, 7.4.0 - 7.4.33
Notice: Undefined variable: cell in /in/sbk1W on line 44 Notice: Trying to get property 'nodeValue' of non-object in /in/sbk1W on line 44 Notice: Undefined variable: cell in /in/sbk1W on line 44 Notice: Trying to get property 'nodeValue' of non-object in /in/sbk1W on line 44 Notice: Undefined variable: cell in /in/sbk1W on line 44 Notice: Trying to get property 'nodeValue' of non-object in /in/sbk1W on line 44 Notice: Undefined variable: cell in /in/sbk1W on line 44 Notice: Trying to get property 'nodeValue' of non-object in /in/sbk1W on line 44 Notice: Undefined variable: cell in /in/sbk1W on line 44 Notice: Trying to get property 'nodeValue' of non-object in /in/sbk1W on line 44 Array ( [0] => [1] => [2] => [3] => [4] => ) Array ( [0] => Array ( [] => UPC ) [1] => Array ( [] => ) [2] => Array ( [] => ) )
Output for 7.3.32 - 7.3.33
Array ( [0] => [1] => [2] => [3] => [4] => ) Array ( [0] => Array ( [] => UPC ) [1] => Array ( [] => ) [2] => Array ( [] => ) )
Output for 5.6.38, 7.1.0 - 7.1.23
Notice: Undefined variable: cell in /in/sbk1W on line 44 Notice: Trying to get property of non-object in /in/sbk1W on line 44 Notice: Undefined variable: cell in /in/sbk1W on line 44 Notice: Trying to get property of non-object in /in/sbk1W on line 44 Notice: Undefined variable: cell in /in/sbk1W on line 44 Notice: Trying to get property of non-object in /in/sbk1W on line 44 Notice: Undefined variable: cell in /in/sbk1W on line 44 Notice: Trying to get property of non-object in /in/sbk1W on line 44 Notice: Undefined variable: cell in /in/sbk1W on line 44 Notice: Trying to get property of non-object in /in/sbk1W on line 44 Array ( [0] => [1] => [2] => [3] => [4] => ) Array ( [0] => Array ( [] => UPC ) [1] => Array ( [] => ) [2] => Array ( [] => ) )

preferences:
158.88 ms | 403 KiB | 169 Q