3v4l.org

run code in 300+ PHP versions simultaneously
<?php $xml = '<products> <product> <categoryName>Kable i konwertery AV</categoryName> <brandName>Belkin</brandName> <productCode>AV10176bt1M-BLK</productCode> <productId>5616488</productId> <productFullName>Kabel Belkin Kabel HDMI Ultra HD High Speed 1m-AV10176bt1M-BLK</productFullName> <productEan>0745883767465</productEan> <productEuroPriceNetto>59.71</productEuroPriceNetto> <productFrontendPriceNetto>258.54</productFrontendPriceNetto> <productFastestSupplierQuantity>23</productFastestSupplierQuantity> <deliveryEstimatedDays>2</deliveryEstimatedDays> </product> <product> <categoryName>Telewizory</categoryName> <brandName>Sony</brandName> <productCode>KDL32WD757SAEP</productCode> <productId>1005662</productId> <productFullName>Telewizor Sony KDL-32WD757 SAEP</productFullName> <productEan></productEan> <productEuroPriceNetto>412.33</productEuroPriceNetto> <productFrontendPriceNetto>1785.38</productFrontendPriceNetto> <productFastestSupplierQuantity>11</productFastestSupplierQuantity> <deliveryEstimatedDays>6</deliveryEstimatedDays> </product> <product> <categoryName>Kuchnie i akcesoria</categoryName> <brandName>Brimarex</brandName> <productCode>1566287</productCode> <productId>885156</productId> <productFullName>Brimarex Drewniane owoce, Kiwi - 1566287</productFullName> <productEan></productEan> <productEuroPriceNetto>0.7</productEuroPriceNetto> <productFrontendPriceNetto>3.05</productFrontendPriceNetto> <productFastestSupplierQuantity>7</productFastestSupplierQuantity> <deliveryEstimatedDays>3</deliveryEstimatedDays> </product> </products>'; $reader = new XMLReader(); $reader->xml($xml); $count = 0; $this_value = ''; $products = array(); while($reader->read()) { switch ($reader->nodeType) { case XMLReader::ELEMENT: // deal with self-closing tags e.g. <productEan /> if ($reader->isEmptyElement) { ${$reader->name} = ''; $products[$count][$reader->name] = ''; } break; case XMLReader::TEXT: case XMLReader::CDATA: // save the value for storage when we get to the end of the element $this_value = $reader->value; break; case XMLReader::END_ELEMENT: if ($reader->name == 'product') { $count++; print_r(array($categoryName, $brandName, $productCode, $productId, $productFullName, $productEan, $productEuroPriceNetto, $productFrontendPriceNetto, $productFastestSupplierQuantity, $deliveryEstimatedDays)); } elseif ($reader->name != 'products') { ${$reader->name} = $this_value; $products[$count][$reader->name] = $this_value; // set this_value to a blank string to allow for empty tags $this_value = ''; } break; case XMLReader::WHITESPACE: case XMLReader::SIGNIFICANT_WHITESPACE: default: // nothing to do break; } } $reader->close(); print_r($products);
Output for 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.4 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.4, 8.3.6 - 8.3.7
Array ( [0] => Kable i konwertery AV [1] => Belkin [2] => AV10176bt1M-BLK [3] => 5616488 [4] => Kabel Belkin Kabel HDMI Ultra HD High Speed 1m-AV10176bt1M-BLK [5] => 0745883767465 [6] => 59.71 [7] => 258.54 [8] => 23 [9] => 2 ) Array ( [0] => Telewizory [1] => Sony [2] => KDL32WD757SAEP [3] => 1005662 [4] => Telewizor Sony KDL-32WD757 SAEP [5] => [6] => 412.33 [7] => 1785.38 [8] => 11 [9] => 6 ) Array ( [0] => Kuchnie i akcesoria [1] => Brimarex [2] => 1566287 [3] => 885156 [4] => Brimarex Drewniane owoce, Kiwi - 1566287 [5] => [6] => 0.7 [7] => 3.05 [8] => 7 [9] => 3 ) Array ( [0] => Array ( [categoryName] => Kable i konwertery AV [brandName] => Belkin [productCode] => AV10176bt1M-BLK [productId] => 5616488 [productFullName] => Kabel Belkin Kabel HDMI Ultra HD High Speed 1m-AV10176bt1M-BLK [productEan] => 0745883767465 [productEuroPriceNetto] => 59.71 [productFrontendPriceNetto] => 258.54 [productFastestSupplierQuantity] => 23 [deliveryEstimatedDays] => 2 ) [1] => Array ( [categoryName] => Telewizory [brandName] => Sony [productCode] => KDL32WD757SAEP [productId] => 1005662 [productFullName] => Telewizor Sony KDL-32WD757 SAEP [productEan] => [productEuroPriceNetto] => 412.33 [productFrontendPriceNetto] => 1785.38 [productFastestSupplierQuantity] => 11 [deliveryEstimatedDays] => 6 ) [2] => Array ( [categoryName] => Kuchnie i akcesoria [brandName] => Brimarex [productCode] => 1566287 [productId] => 885156 [productFullName] => Brimarex Drewniane owoce, Kiwi - 1566287 [productEan] => [productEuroPriceNetto] => 0.7 [productFrontendPriceNetto] => 3.05 [productFastestSupplierQuantity] => 7 [deliveryEstimatedDays] => 3 ) )
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Array ( [0] => Kable i konwertery AV [1] => Belkin [2] => AV10176bt1M-BLK [3] => 5616488 [4] => Kabel Belkin Kabel HDMI Ultra HD High Speed 1m-AV10176bt1M-BLK [5] => 0745883767465 [6] => 59.71 [7] => 258.54 [8] => 23 [9] => 2 ) Array ( [0] => Telewizory [1] => Sony [2] => KDL32WD757SAEP [3] => 1005662 [4] => Telewizor Sony KDL-32WD757 SAEP [5] => [6] => 412.33 [7] => 1785.38 [8] => 11 [9] => 6 ) Array ( [0] => Kuchnie i akcesoria [1] => Brimarex [2] => 1566287 [3] => 885156 [4] => Brimarex Drewniane owoce, Kiwi - 1566287 [5] => [6] => 0.7 [7] => 3.05 [8] => 7 [9] => 3 ) Array ( [0] => Array ( [categoryName] => Kable i konwertery AV [brandName] => Belkin [productCode] => AV10176bt1M-BLK [productId] => 5616488 [productFullName] => Kabel Belkin Kabel HDMI Ultra HD High Speed 1m-AV10176bt1M-BLK [productEan] => 0745883767465 [productEuroPriceNetto] => 59.71 [productFrontendPriceNetto] => 258.54 [productFastestSupplierQuantity] => 23 [deliveryEstimatedDays] => 2 ) [1] => Array ( [categoryName] => Telewizory [brandName] => Sony [productCode] => KDL32WD757SAEP [productId] => 1005662 [productFullName] => Telewizor Sony KDL-32WD757 SAEP [productEan] => [productEuroPriceNetto] => 412.33 [productFrontendPriceNetto] => 1785.38 [productFastestSupplierQuantity] => 11 [deliveryEstimatedDays] => 6 ) [2] => Array ( [categoryName] => Kuchnie i akcesoria [brandName] => Brimarex [productCode] => 1566287 [productId] => 885156 [productFullName] => Brimarex Drewniane owoce, Kiwi - 1566287 [productEan] => [productEuroPriceNetto] => 0.7 [productFrontendPriceNetto] => 3.05 [productFastestSupplierQuantity] => 7 [deliveryEstimatedDays] => 3 ) )

preferences:
131.9 ms | 409 KiB | 157 Q