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";
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Deprecated: Return type of JsonSimpleXMLElementDecorator::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/XOiVD on line 24 { "@attributes": { "attribute": "variable" }, "sampleElement": [ { "sampleChild": [ null, { "@attributes": { "attribute": "variable" }, "@text": "text" } ], "@text": "Text" }, "test" ] }
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.10, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
{ "@attributes": { "attribute": "variable" }, "sampleElement": [ { "sampleChild": [ null, { "@attributes": { "attribute": "variable" }, "@text": "text" } ], "@text": "Text" }, "test" ] }
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/XOiVD on line 43
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/XOiVD on line 43
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting '{' in /in/XOiVD on line 8
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting '{' in /in/XOiVD on line 8
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `'{'' in /in/XOiVD on line 8
Process exited with code 255.

preferences:
236.34 ms | 401 KiB | 350 Q