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.01116.84
8.3.50.0110.00722.07
8.3.40.0120.00919.04
8.3.30.0030.01219.41
8.3.20.0090.00020.45
8.3.10.0040.00422.32
8.3.00.0000.00819.62
8.2.180.0040.01118.54
8.2.170.0140.00022.96
8.2.160.0070.00720.78
8.2.150.0040.00424.18
8.2.140.0050.00324.66
8.2.130.0040.00426.16
8.2.120.0040.00420.02
8.2.110.0000.01019.39
8.2.100.0080.00418.00
8.2.90.0040.00419.59
8.2.80.0000.00817.97
8.2.70.0080.00017.75
8.2.60.0050.00318.16
8.2.50.0080.00018.07
8.2.40.0000.00820.01
8.2.30.0040.00418.32
8.2.20.0000.00717.92
8.2.10.0040.00418.43
8.2.00.0060.00318.00
8.1.280.0040.01525.92
8.1.270.0120.00418.99
8.1.260.0080.00026.35
8.1.250.0060.00328.09
8.1.240.0030.00622.27
8.1.230.0120.00019.51
8.1.220.0000.00818.04
8.1.210.0000.00818.77
8.1.200.0070.00317.48
8.1.190.0060.00317.61
8.1.180.0030.00618.10
8.1.170.0000.00820.69
8.1.160.0080.00022.20
8.1.150.0000.00819.10
8.1.140.0040.00417.85
8.1.130.0000.00717.96
8.1.120.0040.00317.66
8.1.110.0000.00817.66
8.1.100.0000.00817.61
8.1.90.0030.00417.60
8.1.80.0050.00317.56
8.1.70.0030.00317.69
8.1.60.0030.00717.69
8.1.50.0000.00817.62
8.1.40.0030.00517.88
8.1.30.0030.00617.74
8.1.20.0030.00517.82
8.1.10.0040.00417.79
8.1.00.0040.00417.73
8.0.300.0040.00418.77
8.0.290.0040.00417.00
8.0.280.0030.00318.59
8.0.270.0040.00417.54
8.0.260.0040.00417.57
8.0.250.0070.00017.13
8.0.240.0030.00317.24
8.0.230.0080.00017.22
8.0.220.0070.00017.07
8.0.210.0030.00317.13
8.0.200.0040.00417.24
8.0.190.0040.00417.13
8.0.180.0000.00717.23
8.0.170.0090.00017.11
8.0.160.0070.00017.26
8.0.150.0040.00417.10
8.0.140.0020.00517.05
8.0.130.0030.00313.70
8.0.120.0000.00717.17
8.0.110.0040.00417.09
8.0.100.0060.00317.21
8.0.90.0040.00416.99
8.0.80.0100.00617.13
8.0.70.0000.00817.04
8.0.60.0050.00217.20
8.0.50.0040.00417.05
8.0.30.0110.01017.32
8.0.20.0110.00817.40
8.0.10.0040.00417.34
8.0.00.0110.00517.06
7.4.330.0050.00015.53
7.4.320.0040.00416.85
7.4.300.0000.00616.56
7.4.290.0080.00016.69
7.4.280.0030.00516.75
7.4.270.0070.00016.91
7.4.260.0040.00416.74
7.4.250.0040.00416.60
7.4.240.0060.00216.69
7.4.230.0030.00316.91
7.4.220.0090.00916.89
7.4.210.0030.01016.92
7.4.200.0040.00416.57
7.4.160.0160.00316.88
7.4.150.0260.01517.40
7.4.140.0080.01017.86
7.4.130.0070.01316.72
7.4.120.0030.01316.85
7.4.110.0140.01116.78
7.4.100.0090.01216.75
7.4.90.0130.00316.65
7.4.80.0130.00519.39
7.4.70.0070.01116.65
7.4.60.0080.00916.76
7.4.50.0030.00316.71
7.4.40.0110.00716.63
7.4.30.0100.01016.62
7.4.10.0060.01015.34
7.4.00.0130.00414.91
7.3.330.0000.00713.48
7.3.320.0000.01313.55
7.3.310.0030.00516.38
7.3.300.0030.00316.50
7.3.290.0060.00816.49
7.3.280.0090.00916.50
7.3.270.0070.01017.40
7.3.260.0160.00716.61
7.3.250.0170.00316.62
7.3.240.0100.01316.72
7.3.230.0150.00416.70
7.3.210.0150.00616.55
7.3.200.0080.00819.39
7.3.190.0100.00616.71
7.3.180.0030.01316.70
7.3.170.0070.01116.47
7.3.160.0120.00416.54
7.3.130.0070.01115.21
7.3.120.0100.00714.81
7.3.110.0100.01014.83
7.3.100.0090.00614.79
7.3.90.0100.00714.78
7.3.80.0040.00814.65
7.3.70.0040.00414.82
7.3.60.0140.00014.91
7.3.50.0140.00015.04
7.3.40.0060.00315.01
7.3.30.0070.01114.74
7.3.20.0040.01216.71
7.3.10.0080.00616.83
7.3.00.0060.00816.73
7.2.330.0160.00316.55
7.2.320.0070.01416.81
7.2.310.0070.01416.70
7.2.300.0180.00616.90
7.2.290.0110.00616.50
7.2.260.0090.00915.28
7.2.250.0070.01015.10
7.2.240.0070.00715.10
7.2.230.0030.01315.02
7.2.220.0100.01014.77
7.2.210.0060.00315.05
7.2.200.0100.00315.21
7.2.190.0080.00815.12
7.2.180.0040.01115.03
7.2.170.0060.00814.77
7.2.160.0070.01014.88
7.2.150.0090.00316.85
7.2.140.0130.00316.94
7.2.130.0060.00916.94
7.2.120.0070.00816.86
7.2.110.0060.00816.98
7.2.100.0110.00516.89
7.2.90.0100.00616.94
7.2.80.0080.00616.81
7.2.70.0070.00816.77
7.2.60.0070.00716.99
7.2.50.0040.00716.90
7.2.40.0040.00916.99
7.2.30.0040.01016.96
7.2.20.0100.00516.73
7.2.10.0070.00816.76
7.2.00.0090.00317.76
7.1.330.0070.00715.71
7.1.320.0060.00915.65
7.1.310.0040.00815.61
7.1.300.0040.01415.84
7.1.290.0100.00715.69
7.1.280.0100.00315.94
7.1.270.0030.01315.82
7.1.260.0000.00815.75
7.1.250.0060.00615.82
7.1.240.0030.01015.95
7.1.230.0070.01015.80
7.1.220.0070.00315.75
7.1.210.0040.00815.88
7.1.200.0030.01015.87
7.1.190.0080.00315.89
7.1.180.0030.01015.89
7.1.170.0000.01115.98
7.1.160.0030.00615.95
7.1.150.0000.01315.94
7.1.140.0090.00315.83
7.1.130.0080.00615.77
7.1.120.0100.00015.79
7.1.110.0080.00615.91
7.1.100.0070.00617.09
7.1.90.0070.00715.95
7.1.80.0090.00615.87
7.1.70.0020.00816.56
7.1.60.0110.00817.63
7.1.50.0060.00916.41
7.1.40.0000.01415.76
7.1.30.0070.00715.92
7.1.20.0060.00915.77
7.1.10.0000.01315.97
7.1.00.0050.04119.25
7.0.330.0080.00815.47
7.0.320.0040.01115.62
7.0.310.0070.00715.44
7.0.300.0060.01015.30
7.0.290.0100.00315.36
7.0.280.0070.00315.59
7.0.270.0030.00715.48
7.0.260.0030.00615.53
7.0.250.0080.00315.40
7.0.240.0000.00915.57
7.0.230.0000.00815.41
7.0.220.0090.00615.48
7.0.210.0100.00715.45
7.0.200.0220.00615.18
7.0.190.0030.01315.42
7.0.180.0040.01115.54
7.0.170.0040.00715.53
7.0.160.0120.00015.23
7.0.150.0040.01115.62
7.0.140.0010.03918.80
7.0.130.0030.01015.62
7.0.120.0000.01215.66
7.0.110.0000.01015.51
7.0.100.0080.00815.55
7.0.90.0240.03217.73
7.0.80.0300.04117.73
7.0.70.0270.02317.77
7.0.60.0320.04017.79
7.0.50.0200.04618.01
7.0.40.0100.03816.84
7.0.30.0110.03816.95
7.0.20.0030.04816.81
7.0.10.0060.04316.81
7.0.00.0070.03816.84
5.6.400.0120.00314.45
5.6.390.0070.01014.47
5.6.380.0080.00414.67
5.6.370.0000.01414.47
5.6.360.0030.01014.45
5.6.350.0080.00414.56
5.6.340.0030.00614.50
5.6.330.0040.00414.59
5.6.320.0000.01414.86
5.6.310.0030.00714.53
5.6.300.0000.01114.15
5.6.290.0030.00714.63
5.6.280.0080.03717.67
5.6.270.0070.00314.47
5.6.260.0060.00614.55
5.6.250.0030.00614.33
5.6.240.0050.04617.52
5.6.230.0070.04017.54
5.6.220.0050.03417.51
5.6.210.0080.04517.54
5.6.200.0080.04517.81
5.6.190.0070.04417.73
5.6.180.0050.05017.74
5.6.170.0080.02617.78
5.6.160.0090.03817.62
5.6.150.0030.04517.85
5.6.140.0050.05017.70
5.6.130.0090.02817.71
5.6.120.0020.04917.80
5.6.110.0030.03917.77
5.6.100.0080.04817.77
5.6.90.0070.03017.76
5.6.80.0060.02517.56
5.6.70.0050.02917.35
5.6.60.0080.04017.30
5.6.50.0100.03817.42
5.6.40.0070.04417.56
5.6.30.0050.04617.47
5.6.20.0050.04317.34
5.6.10.0090.04017.62
5.6.00.0070.04517.24
5.5.380.0060.02317.33
5.5.370.0080.02317.48
5.5.360.0050.03017.47
5.5.350.0100.04217.51
5.5.340.0050.04317.54
5.5.330.0020.05017.57
5.5.320.0080.04517.59
5.5.310.0070.04617.69
5.5.300.0060.05017.65
5.5.290.0030.05217.65
5.5.280.0110.03017.54
5.5.270.0070.03217.58
5.5.260.0080.04517.52
5.5.250.0040.03217.54
5.5.240.0070.02417.31
5.5.230.0050.04417.40
5.5.220.0050.04217.08
5.5.210.0100.03717.30
5.5.200.0050.03717.30
5.5.190.0050.04717.33
5.5.180.0070.04417.19
5.5.170.0040.01514.25
5.5.160.0110.03517.26
5.5.150.0050.04617.22
5.5.140.0080.03117.22
5.5.130.0080.03617.41
5.5.120.0050.02917.22
5.5.110.0060.04517.36
5.5.100.0070.04017.23
5.5.90.0080.04217.22
5.5.80.0060.03117.33
5.5.70.0060.02617.13
5.5.60.0050.03417.22
5.5.50.0180.03217.20
5.5.40.0050.03517.23
5.5.30.0060.03717.26
5.5.20.0090.02617.29
5.5.10.0070.04217.21
5.5.00.0110.04117.28
5.4.450.0050.04315.20
5.4.440.0030.03615.25
5.4.430.0030.02515.18
5.4.420.0070.04415.37
5.4.410.0070.03415.00
5.4.400.0050.04014.98
5.4.390.0070.03115.18
5.4.380.0130.02815.11
5.4.370.0030.03215.08
5.4.360.0050.02814.92
5.4.350.0070.04015.04
5.4.340.0020.03915.07
5.4.330.0060.00611.01
5.4.320.0080.02715.10
5.4.310.0030.04715.27
5.4.300.0050.02715.01
5.4.290.0030.02715.24
5.4.280.0040.02415.25
5.4.270.0050.04515.00
5.4.260.0030.02315.18
5.4.250.0050.04014.97
5.4.240.0030.03515.12
5.4.230.0060.03814.91
5.4.220.0080.03315.02
5.4.210.0050.04715.02
5.4.200.0090.03215.24
5.4.190.0090.03615.02
5.4.180.0080.03215.08
5.4.170.0050.03515.20
5.4.160.0030.03015.15
5.4.150.0120.03515.02
5.4.140.0030.04013.75
5.4.130.0120.03513.87
5.4.120.0090.02213.77
5.4.110.0050.02513.89
5.4.100.0030.03813.69
5.4.90.0050.03713.82
5.4.80.0050.03913.82
5.4.70.0070.03913.97
5.4.60.0050.03113.85
5.4.50.0020.04413.74
5.4.40.0050.02813.65
5.4.30.0030.04213.83
5.4.20.0050.04113.88
5.4.10.0070.04413.84
5.4.00.0050.04013.48
5.3.290.0030.08014.73
5.3.280.0200.06314.53
5.3.270.0100.07014.70
5.3.260.0030.07714.60
5.3.250.0100.04314.54
5.3.240.0000.05314.73
5.3.230.0100.04314.58
5.3.220.0070.07014.71
5.3.210.0030.07714.64
5.3.200.0070.08014.54
5.3.190.0130.07014.50
5.3.180.0100.07714.63
5.3.170.0070.07014.52
5.3.160.0030.05014.66
5.3.150.0030.05014.50
5.3.140.0100.07314.63
5.3.130.0100.07014.55
5.3.120.0030.04714.48
5.3.110.0100.05314.54
5.3.100.0000.08314.02
5.3.90.0070.07314.08
5.3.80.0000.05714.13
5.3.70.0070.07314.05
5.3.60.0070.08014.11
5.3.50.0070.06713.93
5.3.40.0070.07714.05
5.3.30.0030.05014.06
5.3.20.0030.04013.82
5.3.10.0030.03013.78
5.3.00.0000.03713.75
5.2.170.0030.06311.29
5.2.160.0030.03011.08
5.2.150.0070.03311.17
5.2.140.0030.04011.20
5.2.130.0000.03011.14
5.2.120.0000.05311.04
5.2.110.0000.03011.25
5.2.100.0000.03010.96
5.2.90.0030.03711.12
5.2.80.0000.03011.11
5.2.70.0070.02311.13
5.2.60.0000.03010.99
5.2.50.0030.02310.92
5.2.40.0000.03011.09
5.2.30.0000.03011.07
5.2.20.0030.02710.87
5.2.10.0000.03010.93
5.2.00.0000.02710.78
5.1.60.0070.04010.60
5.1.50.0130.01710.60
5.1.40.0030.02310.60
5.1.30.0070.02010.60
5.1.20.0070.02310.60
5.1.10.0000.02710.60
5.1.00.0000.02710.60
5.0.50.0030.03710.60
5.0.40.0030.02010.60
5.0.30.0000.03310.60
5.0.20.0030.03010.60
5.0.10.0030.02310.60
5.0.00.0030.03310.60
4.4.90.0030.01310.60
4.4.80.0000.02010.60
4.4.70.0000.01710.60
4.4.60.0000.02010.60
4.4.50.0000.01710.60
4.4.40.0000.03710.60
4.4.30.0030.01310.60
4.4.20.0070.01010.60
4.4.10.0030.01010.60
4.4.00.0030.02310.60
4.3.110.0000.01710.60
4.3.100.0070.02010.60
4.3.90.0000.01710.60
4.3.80.0000.02310.60
4.3.70.0070.01010.60
4.3.60.0000.01310.60
4.3.50.0000.01710.60
4.3.40.0000.02710.60
4.3.30.0000.02010.60
4.3.20.0000.01710.60
4.3.10.0000.02010.60
4.3.00.0000.01310.60

preferences:
52.33 ms | 401 KiB | 5 Q