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 attribute="variable">text</sampleChild> </sampleElement> <sampleElement> test </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.0170.00316.88
8.3.50.0120.00722.13
8.3.40.0070.00719.09
8.3.30.0040.01119.47
8.3.20.0090.00020.46
8.3.10.0000.00822.02
8.3.00.0040.00419.63
8.2.180.0120.00318.52
8.2.170.0140.00722.96
8.2.160.0130.00020.68
8.2.150.0040.00424.18
8.2.140.0040.00424.66
8.2.130.0080.00026.16
8.2.120.0050.00319.79
8.2.110.0100.00020.77
8.2.100.0040.00817.91
8.2.90.0000.00818.09
8.2.80.0000.00921.34
8.2.70.0030.00517.75
8.2.60.0060.00317.75
8.2.50.0000.00817.88
8.2.40.0000.00820.14
8.2.30.0000.00718.46
8.2.20.0040.00418.02
8.2.10.0090.00018.34
8.2.00.0000.00717.86
8.1.280.0150.00325.92
8.1.270.0060.00320.77
8.1.260.0000.00826.35
8.1.250.0030.00628.09
8.1.240.0060.00322.79
8.1.230.0000.01222.90
8.1.220.0080.00018.00
8.1.210.0050.00518.91
8.1.200.0000.00817.48
8.1.190.0080.00017.60
8.1.180.0000.00818.10
8.1.170.0030.00520.49
8.1.160.0080.00022.24
8.1.150.0040.00419.12
8.1.140.0020.00517.68
8.1.130.0030.00318.05
8.1.120.0040.00417.71
8.1.110.0040.00417.59
8.1.100.0070.00017.64
8.1.90.0000.00717.54
8.1.80.0000.00717.62
8.1.70.0000.00717.69
8.1.60.0000.01017.70
8.1.50.0060.00317.79
8.1.40.0040.00417.72
8.1.30.0030.00517.84
8.1.20.0040.00417.83
8.1.10.0030.00517.69
8.1.00.0080.00017.74
8.0.300.0000.00720.23
8.0.290.0030.00617.00
8.0.280.0040.00418.64
8.0.270.0030.00317.52
8.0.260.0070.00017.48
8.0.250.0030.00317.28
8.0.240.0060.00017.19
8.0.230.0040.00417.22
8.0.220.0000.00717.09
8.0.210.0030.00417.14
8.0.200.0000.00717.16
8.0.190.0040.00417.33
8.0.180.0000.00717.13
8.0.170.0040.00417.15
8.0.160.0050.00317.07
8.0.150.0000.00817.17
8.0.140.0040.00417.00
8.0.130.0030.00313.67
8.0.120.0040.00417.16
8.0.110.0000.00717.06
8.0.100.0070.00017.10
8.0.90.0000.00717.23
8.0.80.0060.00917.19
8.0.70.0030.00517.15
8.0.60.0070.00016.99
8.0.50.0080.00017.25
8.0.30.0140.00917.40
8.0.20.0110.00717.40
8.0.10.0000.00717.23
8.0.00.0110.00616.92
7.4.330.0000.00515.25
7.4.320.0030.00316.66
7.4.300.0060.00016.90
7.4.290.0050.00216.72
7.4.280.0040.00416.79
7.4.270.0040.00416.64
7.4.260.0070.00016.69
7.4.250.0000.00716.82
7.4.240.0020.00616.67
7.4.230.0030.00316.91
7.4.220.0150.00416.82
7.4.210.0090.00916.83
7.4.200.0000.00716.91
7.4.160.0100.01016.78
7.4.150.0090.00917.40
7.4.140.0120.00517.86
7.4.130.0110.00816.71
7.4.120.0150.00316.71
7.4.110.0100.01016.51
7.4.100.0160.00916.68
7.4.90.0030.01516.86
7.4.80.0040.01419.39
7.4.70.0110.01116.63
7.4.60.0110.00516.63
7.4.50.0050.00316.82
7.4.40.0070.01616.57
7.4.30.0040.01516.66
7.4.00.0130.00014.82
7.3.330.0000.00813.61
7.3.320.0100.00313.58
7.3.310.0030.00316.41
7.3.300.0000.00716.45
7.3.290.0070.00916.59
7.3.280.0090.00816.59
7.3.270.0090.00917.40
7.3.260.0170.00016.82
7.3.250.0140.00416.67
7.3.240.0210.00316.70
7.3.230.0160.00616.64
7.3.210.0030.01516.69
7.3.200.0060.01219.39
7.3.190.0140.00716.73
7.3.180.0100.00616.37
7.3.170.0120.00616.55
7.3.160.0200.00316.71
7.2.330.0130.00417.02
7.2.320.0120.00816.64
7.2.310.0070.01016.98
7.2.300.0130.01216.83
7.2.290.0030.01816.82
7.2.00.0140.00319.40
7.1.100.0080.00818.18
7.1.70.0120.00017.14
7.1.60.0100.01319.46
7.1.50.0100.01016.75
7.1.00.0030.07722.47
7.0.200.0480.00914.46
7.0.140.0000.05722.14
7.0.90.0070.08719.93
7.0.80.0070.08019.91
7.0.70.0070.07019.97
7.0.60.0070.07719.92
7.0.50.0130.04320.41
7.0.40.0000.05720.04
7.0.30.0170.07320.19
7.0.20.0100.05020.13
7.0.10.0070.07720.22
7.0.00.0100.04020.21
5.6.280.0030.07321.04
5.6.240.0070.08320.72
5.6.230.0130.08020.78
5.6.220.0130.08020.82
5.6.210.0070.06320.64
5.6.200.0170.08021.04
5.6.190.0070.05021.07
5.6.180.0230.06321.29
5.6.170.0100.08321.20
5.6.160.0030.05721.04
5.6.150.0100.07720.99
5.6.140.0130.07721.24
5.6.130.0070.04321.07
5.6.120.0030.08321.16
5.6.110.0030.08321.01
5.6.100.0230.04021.09
5.6.90.0200.07721.26
5.6.80.0100.03720.50
5.6.70.0300.05720.40
5.6.60.0100.08020.51
5.6.50.0070.08720.63
5.6.40.0100.08020.43
5.6.30.0100.04320.49
5.6.20.0070.04720.64
5.6.10.0200.04020.47
5.6.00.0130.04720.45
5.5.380.0030.08320.62
5.5.370.0100.07320.45
5.5.360.0130.07720.40
5.5.350.0070.07720.45
5.5.340.0100.04720.99
5.5.330.0130.07020.82
5.5.320.0030.05720.93
5.5.310.0030.08720.92
5.5.300.0030.05320.84
5.5.290.0100.05320.89
5.5.280.0100.06021.00
5.5.270.0130.08020.96
5.5.260.0070.05720.96
5.5.250.0030.06020.87
5.5.240.0030.06320.35
5.5.230.0030.08320.34
5.5.220.0100.07320.33
5.5.210.0070.05720.30
5.5.200.0070.07720.30
5.5.190.0100.07720.20
5.5.180.0200.06320.14
5.5.160.0070.03720.29
5.5.150.0070.07720.33
5.5.140.0170.07320.21
5.5.130.0030.05020.16
5.5.120.0230.03020.29
5.5.110.0100.07720.22
5.5.100.0100.05320.24
5.5.90.0070.06020.03
5.5.80.0130.07320.27
5.5.70.0030.08320.13
5.5.60.0070.06320.28
5.5.50.0070.05020.02
5.5.40.0100.07020.19
5.5.30.0100.07020.12
5.5.20.0130.07020.24
5.5.10.0130.07320.11
5.5.00.0030.07020.21
5.4.450.0100.06319.59
5.4.440.0100.06719.25
5.4.430.0070.05319.24
5.4.420.0130.07319.27
5.4.410.0070.07319.39
5.4.400.0070.05019.24
5.4.390.0100.04318.92
5.4.380.0170.04718.92
5.4.370.0000.08019.36
5.4.360.0100.06318.94
5.4.350.0100.07719.23
5.4.340.0100.07718.92
5.4.320.0130.07719.37
5.4.310.0100.04719.23
5.4.300.0100.07018.99
5.4.290.0030.08019.25
5.4.280.0130.04019.25
5.4.270.0070.07719.01
5.4.260.0230.05719.09
5.4.250.0170.06718.91
5.4.240.0070.07318.90
5.4.230.0070.06319.03
5.4.220.0130.06719.27
5.4.210.0170.03019.19
5.4.200.0100.06719.27
5.4.190.0100.05019.26
5.4.180.0130.06319.28
5.4.170.0070.04719.35
5.4.160.0070.07019.10
5.4.150.0200.06319.22
5.4.140.0000.06016.49
5.4.130.0030.06316.32
5.4.120.0170.06716.54
5.4.110.0130.06716.63
5.4.100.0100.06316.55
5.4.90.0030.05316.48
5.4.80.0170.04016.53
5.4.70.0070.07016.47
5.4.60.0100.03716.52
5.4.50.0030.07316.64
5.4.40.0130.05716.46
5.4.30.0100.06716.51
5.4.20.0070.07016.57
5.4.10.0070.07016.45
5.4.00.0000.05316.04
5.3.290.0070.06714.70
5.3.280.0070.05314.71
5.3.270.0070.06014.63
5.3.260.0030.05014.71
5.3.250.0030.06714.67
5.3.240.0070.07714.58
5.3.230.0100.07314.55
5.3.220.0200.06014.66
5.3.210.0070.07714.50
5.3.200.0100.07014.71
5.3.190.0100.04014.50
5.3.180.0170.06314.58
5.3.170.0100.07714.57
5.3.160.0070.07714.64
5.3.150.0200.06314.63
5.3.140.0070.07014.51
5.3.130.0070.07314.52
5.3.120.0200.06714.58
5.3.110.0030.05314.66
5.3.100.0030.03713.98
5.3.90.0030.08014.04
5.3.80.0130.07314.00
5.3.70.0070.08014.09
5.3.60.0130.06714.05
5.3.50.0100.05313.97
5.3.40.0100.07014.02
5.3.30.0100.03714.02
5.3.20.0100.06713.84
5.3.10.0070.04713.55
5.3.00.0030.07313.63
5.2.170.0100.05711.08
5.2.160.0170.05011.04
5.2.150.0000.06011.22
5.2.140.0070.05711.27
5.2.130.0100.05011.10
5.2.120.0070.05711.10
5.2.110.0030.04011.00
5.2.100.0070.06311.04
5.2.90.0030.06011.25
5.2.80.0070.04310.99
5.2.70.0030.06011.23
5.2.60.0030.05310.95
5.2.50.0000.04010.89
5.2.40.0070.05710.86
5.2.30.0000.04311.05
5.2.20.0000.06011.07
5.2.10.0070.04310.99
5.2.00.0200.04310.71
5.1.60.0030.0539.99
5.1.50.0100.04310.13
5.1.40.0030.04010.10
5.1.30.0030.05710.20
5.1.20.0070.05710.48
5.1.10.0030.04010.17
5.1.00.0100.05010.18
5.0.50.0030.0438.40
5.0.40.0070.0408.50
5.0.30.0030.0438.07
5.0.20.0000.0438.32
5.0.10.0030.0438.17
5.0.00.0030.0608.26
4.4.90.0030.0337.19
4.4.80.0030.0237.19
4.4.70.0000.0277.19
4.4.60.0030.0237.19
4.4.50.0000.0307.19
4.4.40.0070.0477.19
4.4.30.0070.0277.19
4.4.20.0100.0307.19
4.4.10.0130.0237.19
4.4.00.0000.0577.19
4.3.110.0000.0337.19
4.3.100.0000.0177.19
4.3.90.0030.0377.19
4.3.80.0000.0437.19
4.3.70.0000.0377.19
4.3.60.0030.0237.19
4.3.50.0030.0277.19
4.3.40.0000.0377.19
4.3.30.0000.0207.19
4.3.20.0070.0337.19
4.3.10.0000.0377.19
4.3.00.0030.0337.19

preferences:
46.45 ms | 401 KiB | 5 Q