3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Class JsonSimpleXMLElementDecorator * * Implement JsonSerializable for SimpleXMLElement as a Decorator */ class JsonSimpleXMLElementDecorator implements JsonSerializable { /** * @var SimpleXMLElement */ private $subject; public function __construct(SimpleXMLElement $element) { $this->subject = $element; } /** * Specify data which should be serialized to JSON * * @return mixed data which can be serialized by json_encode. */ public function jsonSerialize() { $subject = $this->subject; $array = array(); // json encode attributes if any. if ($attributes = $subject->attributes()) { $array['@attributes'] = array_map('strval', iterator_to_array($attributes)); } // traverse into children if applicable $children = $subject; // json encode child elements if any. group on duplicate names as an array. foreach ($children as $name => $element) { $decorator = new self($element); if (isset($array[$name])) { if (!is_array($array[$name])) { $array[$name] = [$array[$name]]; } $array[$name][] = $decorator; } else { $array[$name] = $decorator; } } // json encode non-whitespace element simplexml text values. $text = trim($subject); if (strlen($text)) { if ($array) { $array['@text'] = $text; } else { $array = $text; } } // return empty elements as NULL (self-closing or empty tags) if (!$array) { $array = NULL; } return $array; } } $buffer = '<?xml version="1.0"?> <root attribute="variable"> <sampleElement> Text <sampleChild /> <sampleChild /> </sampleElement> <sampleElement> test <sampleChild attribute="variable">text</sampleChild> </sampleElement> </root>'; $xml = new SimpleXMLElement($buffer); $xml = new JsonSimpleXMLElementDecorator($xml); echo json_encode($xml, JSON_PRETTY_PRINT), "\n";

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0040.01117.11
8.3.50.0340.00322.18
8.3.40.0160.00619.22
8.3.30.0080.00819.42
8.3.20.0050.00320.46
8.3.10.0040.00422.15
8.3.00.0030.00519.75
8.2.180.0090.00618.54
8.2.170.0070.00722.96
8.2.160.0100.00320.46
8.2.150.0080.00024.18
8.2.140.0040.00424.66
8.2.130.0040.00426.16
8.2.120.0030.00619.91
8.2.110.0030.00619.51
8.2.100.0040.00817.88
8.2.90.0080.00019.53
8.2.80.0040.00417.97
8.2.70.0060.00317.88
8.2.60.0030.00518.05
8.2.50.0040.00418.07
8.2.40.0050.00320.20
8.2.30.0030.00618.29
8.2.20.0040.00417.93
8.2.10.0000.00818.29
8.2.00.0050.00517.95
8.1.280.0150.00625.92
8.1.270.0100.00719.21
8.1.260.0040.00426.35
8.1.250.0000.00828.09
8.1.240.0070.00322.37
8.1.230.0120.00017.61
8.1.220.0060.00317.80
8.1.210.0000.00818.77
8.1.200.0060.00317.48
8.1.190.0030.00517.60
8.1.180.0000.00818.10
8.1.170.0050.00320.29
8.1.160.0040.00422.23
8.1.150.0060.00319.16
8.1.140.0070.00017.68
8.1.130.0000.00718.04
8.1.120.0000.00717.58
8.1.110.0030.00517.73
8.1.100.0040.00417.57
8.1.90.0000.00717.57
8.1.80.0040.00417.61
8.1.70.0030.00317.67
8.1.60.0040.00417.70
8.1.50.0050.00317.81
8.1.40.0000.00917.62
8.1.30.0060.00317.93
8.1.20.0040.00417.79
8.1.10.0000.00817.73
8.1.00.0040.00417.77
8.0.300.0000.00818.77
8.0.290.0040.00417.00
8.0.280.0000.00718.70
8.0.270.0000.00717.49
8.0.260.0040.00417.45
8.0.250.0000.00717.21
8.0.240.0030.00317.31
8.0.230.0050.00217.13
8.0.220.0030.00317.10
8.0.210.0000.00717.15
8.0.200.0050.00317.18
8.0.190.0000.00817.13
8.0.180.0040.00417.12
8.0.170.0040.00417.25
8.0.160.0000.00717.05
8.0.150.0040.00417.07
8.0.140.0040.00417.02
8.0.130.0000.00713.59
8.0.120.0040.00417.04
8.0.110.0000.00717.10
8.0.100.0000.00817.06
8.0.90.0040.00417.29
8.0.80.0090.00617.04
8.0.70.0030.00517.09
8.0.60.0000.00816.92
8.0.50.0050.00317.16
8.0.30.0130.00717.41
8.0.20.0110.01017.52
8.0.10.0040.00417.30
8.0.00.0100.00716.97
7.4.330.0000.00515.24
7.4.320.0000.00716.74
7.4.300.0000.00716.68
7.4.290.0000.00716.77
7.4.280.0030.00616.74
7.4.270.0070.00316.78
7.4.260.0040.00416.73
7.4.250.0000.00816.77
7.4.240.0040.00416.65
7.4.230.0000.00716.62
7.4.220.0080.01216.69
7.4.210.0200.00016.76
7.4.200.0000.00816.63
7.4.160.0100.01016.77
7.4.150.0210.00917.40
7.4.140.0070.01217.86
7.4.130.0050.01316.82
7.4.120.0040.01316.74
7.4.110.0110.01216.65
7.4.100.0150.00616.91
7.4.90.0110.00716.64
7.4.80.0060.01219.39
7.4.70.0110.00916.67
7.4.60.0080.00816.77
7.4.50.0030.00616.51
7.4.40.0100.00716.62
7.4.30.0090.00916.64
7.4.00.0090.01014.93
7.3.330.0000.00613.34
7.3.320.0120.00313.57
7.3.310.0070.00016.61
7.3.300.0030.00316.57
7.3.290.0060.00816.60
7.3.280.0090.00916.56
7.3.270.0070.01117.40
7.3.260.0100.00716.78
7.3.250.0130.00516.69
7.3.240.0130.01316.67
7.3.230.0150.00316.70
7.3.210.0090.00916.70
7.3.200.0080.01219.39
7.3.190.0090.00916.70
7.3.180.0060.01016.62
7.3.170.0110.00616.70
7.3.160.0130.00316.74
7.3.120.0100.00515.12
7.3.110.0070.01015.00
7.3.100.0050.01014.83
7.3.90.0060.00915.18
7.3.80.0050.00814.92
7.3.70.0040.00915.11
7.3.60.0060.00914.94
7.3.50.0040.00914.95
7.3.40.0060.00315.00
7.3.30.0050.00914.96
7.3.20.0050.00816.84
7.3.10.0060.00916.73
7.3.00.0060.00816.81
7.2.330.0060.01216.97
7.2.320.0140.00717.03
7.2.310.0140.00416.67
7.2.300.0120.00617.03
7.2.290.0140.00716.77
7.2.250.0030.01415.23
7.2.240.0050.01515.04
7.2.230.0050.01215.04
7.2.220.0050.01015.05
7.2.210.0050.00915.39
7.2.200.0070.00515.21
7.2.190.0070.00515.29
7.2.180.0060.01015.04
7.2.170.0070.00915.29
7.2.130.0030.00617.03
7.2.120.0000.01317.04
7.2.110.0080.00017.12
7.2.100.0060.00917.02
7.2.90.0050.00517.23
7.2.80.0030.00617.24
7.2.70.0030.01016.82
7.2.60.0110.00316.96
7.2.50.0030.01317.00
7.2.40.0110.00416.89
7.2.30.0040.01116.99
7.2.20.0030.01317.07
7.2.10.0030.00817.09
7.2.00.0050.00918.10
7.1.330.0050.01015.65
7.1.320.0070.00915.75
7.1.310.0050.00715.92
7.1.300.0030.00715.90
7.1.290.0040.01015.91
7.1.280.0000.01315.69
7.1.270.0060.00715.90
7.1.260.0050.00715.98
7.1.250.0030.00715.91
7.1.100.0130.00718.32
7.1.70.0000.01017.32
7.1.60.0070.01719.46
7.1.50.0040.00816.95
7.1.00.0070.07322.54
7.0.200.0380.01014.68
7.0.140.0000.07722.11
7.0.90.0130.07719.97
7.0.80.0130.05719.92
7.0.70.0070.04719.89
7.0.60.0170.07720.06
7.0.50.0670.07720.29
7.0.40.0200.02720.13
7.0.30.0070.04020.19
7.0.20.0230.04020.19
7.0.10.0000.05020.17
7.0.00.0070.04320.26
5.6.280.0130.06320.90
5.6.240.0100.07720.72
5.6.230.0070.08320.65
5.6.220.0030.06720.53
5.6.210.0200.06020.70
5.6.200.0100.08321.09
5.6.190.0000.05321.24
5.6.180.0230.06021.14
5.6.170.0130.08021.14
5.6.160.0070.06021.02
5.6.150.0070.05721.02
5.6.140.0130.07321.29
5.6.130.0030.08721.12
5.6.120.0070.08321.19
5.6.110.0030.09021.01
5.6.100.0100.08321.07
5.6.90.0030.04721.15
5.6.80.0100.05020.51
5.6.70.0030.04020.40
5.6.60.0130.04020.52
5.6.50.0070.04320.52
5.6.40.0030.07720.45
5.6.30.0070.07320.46
5.6.20.0170.06720.46
5.6.10.0130.07020.57
5.6.00.0070.05320.48
5.5.380.0000.07320.43
5.5.370.0100.05320.45
5.5.360.0100.06720.63
5.5.350.0000.05020.56
5.5.340.0070.04320.95
5.5.330.0030.05321.03
5.5.320.0130.04720.87
5.5.310.0100.06020.92
5.5.300.0100.08320.85
5.5.290.0070.08020.86
5.5.280.0100.06721.03
5.5.270.0200.07320.89
5.5.260.0030.08321.03
5.5.250.0170.03020.85
5.5.240.0030.04320.37
5.5.230.0070.05020.39
5.5.220.0130.07720.39
5.5.210.0170.07020.26
5.5.200.0100.06020.28
5.5.190.0130.04320.27
5.5.180.0000.05720.21
5.5.160.0070.08020.34
5.5.150.0100.05320.16
5.5.140.0070.05020.24
5.5.130.0030.05720.39
5.5.120.0000.08320.14
5.5.110.0070.04320.40
5.5.100.0070.08020.16
5.5.90.0200.07020.25
5.5.80.0100.07720.07
5.5.70.0170.06020.09
5.5.60.0070.07720.24
5.5.50.0130.04020.15
5.5.40.0070.08020.01
5.5.30.0070.05320.05
5.5.20.0070.04720.11
5.5.10.0170.05020.23
5.5.00.0000.08320.18
5.4.450.0070.08019.29
5.4.440.0030.05719.59
5.4.430.0130.07019.55
5.4.420.0070.04719.56
5.4.410.0100.08019.28
5.4.400.0030.07319.13
5.4.390.0030.04719.25
5.4.380.0070.08319.25
5.4.370.0030.04019.28
5.4.360.0030.04319.22
5.4.350.0070.07719.27
5.4.340.0100.03719.25
5.4.320.0070.07019.24
5.4.310.0030.07719.25
5.4.300.0100.04719.25
5.4.290.0030.04019.26
5.4.280.0030.05019.22
5.4.270.0070.07318.94
5.4.260.0030.05719.11
5.4.250.0100.03718.91
5.4.240.0070.08018.93
5.4.230.0130.07318.93
5.4.220.0070.07319.21
5.4.210.0030.06319.21
5.4.200.0100.07018.94
5.4.190.0070.07319.24
5.4.180.0170.06319.24
5.4.170.0070.04019.27
5.4.160.0170.05319.22
5.4.150.0100.08019.02
5.4.140.0070.06316.50
5.4.130.0030.04316.57
5.4.120.0030.07716.59
5.4.110.0100.06016.59
5.4.100.0030.08016.63
5.4.90.0030.07016.66
5.4.80.0170.07016.50
5.4.70.0030.06016.63
5.4.60.0100.06716.55
5.4.50.0130.04016.56
5.4.40.0070.05316.46
5.4.30.0030.04316.55
5.4.20.0100.05016.47
5.4.10.0070.04716.53
5.4.00.0130.06015.93
5.3.290.0030.05714.61
5.3.280.0100.07314.59
5.3.270.0030.05314.66
5.3.260.0130.05714.73
5.3.250.0030.04314.70
5.3.240.0070.03714.62
5.3.230.0070.06714.62
5.3.220.0030.06014.56
5.3.210.0070.05014.54
5.3.200.0030.05314.71
5.3.190.0070.06314.45
5.3.180.0030.08014.57
5.3.170.0030.08014.49
5.3.160.0100.04314.50
5.3.150.0100.07014.56
5.3.140.0100.06714.63
5.3.130.0100.05714.70
5.3.120.0030.08314.64
5.3.110.0070.08014.54
5.3.100.0030.07314.14
5.3.90.0070.07014.04
5.3.80.0030.05714.09
5.3.70.0070.07713.95
5.3.60.0000.04314.02
5.3.50.0100.07014.00
5.3.40.0070.05714.08
5.3.30.0030.07314.02
5.3.20.0030.04013.76
5.3.10.0000.04013.59
5.3.00.0000.05313.74
5.2.170.0030.06311.24
5.2.160.0030.06311.07
5.2.150.0070.06011.16
5.2.140.0070.05711.05
5.2.130.0030.06011.00
5.2.120.0030.03711.00
5.2.110.0070.05711.02
5.2.100.0030.05710.96
5.2.90.0030.03711.20
5.2.80.0100.02311.03
5.2.70.0030.03011.20
5.2.60.0030.03011.20
5.2.50.0100.06011.15
5.2.40.0070.04711.07
5.2.30.0070.04010.91
5.2.20.0070.03711.06
5.2.10.0070.02710.98
5.2.00.0030.02710.85
5.1.60.0070.02310.09
5.1.50.0030.05310.07
5.1.40.0070.03710.07
5.1.30.0100.05710.47
5.1.20.0030.05310.37
5.1.10.0070.04310.15
5.1.00.0000.03310.09
5.0.50.0030.0408.61
5.0.40.0070.0408.26
5.0.30.0070.0378.30
5.0.20.0000.0278.20
5.0.10.0070.0408.17
5.0.00.0070.0638.05
4.4.90.0030.0237.37
4.4.80.0070.0107.37
4.4.70.0030.0177.37
4.4.60.0000.0207.37
4.4.50.0000.0177.37
4.4.40.0100.0477.37
4.4.30.0030.0307.37
4.4.20.0030.0377.37
4.4.10.0000.0407.37
4.4.00.0000.0477.37
4.3.110.0000.0277.37
4.3.100.0030.0337.37
4.3.90.0030.0277.37
4.3.80.0030.0377.37
4.3.70.0030.0337.37
4.3.60.0030.0337.37
4.3.50.0070.0307.37
4.3.40.0000.0407.37
4.3.30.0070.0237.37
4.3.20.0000.0407.37
4.3.10.0030.0237.37
4.3.00.0000.0407.37

preferences:
57.11 ms | 401 KiB | 5 Q