3v4l.org

run code in 300+ PHP versions simultaneously
<?php const VALUE_FORMAT_RAW = "raw"; const VALUE_FORMAT_TAG_NAME_VALUE_ASSOC = "tagNameValueAssoc"; const VALUE_FORMAT_ARRAY = "array"; function formatArgumentValue(SimpleXMLElement $node, $options) { switch ($options["format"]) { // Associative array from each children of the target node that have the tag name as key and // the tag value as value case VALUE_FORMAT_TAG_NAME_VALUE_ASSOC: $assoc = []; // Recursively get the associative array for the node foreach ($node->children() as $nodePart) { if($nodePart->count() > 0) { $format = VALUE_FORMAT_TAG_NAME_VALUE_ASSOC; if (!empty($nodePart->attributes()->type) && $nodePart->attributes()->type == "array") { $format = VALUE_FORMAT_ARRAY; } $assoc[$nodePart->getName()] = $this->formatArgumentValue($nodePart, ["format" => $format]); } else { // If the value is already defined, we say that the value is an array and add the new value to it if(!empty($assoc[$nodePart->getName()])) { if(!is_array($assoc[$nodePart->getName()])) { $assoc[$nodePart->getName()] = [$assoc[$nodePart->getName()]]; } $assoc[$nodePart->getName()][] = $this->resolveValueString((string) $nodePart); } else { $assoc[$nodePart->getName()] = $this->resolveValueString((string) $nodePart); } } } $argumentValue = $assoc; break; case VALUE_FORMAT_ARRAY: $children = []; $i = 0; foreach($node->children() as $child) { // Get the path for the key $key = $i++; // Set the default key as a regular numbered autoincremented index // NOTE: Here a post-increment is done, the value copied to $key is $i BEFORE // it being incremented. The array will start at 0, even if the increment is done at // the beginning of the string. if(!empty($options['childrenKey'])) { $key = (string) $this->walkNodePath($options['childrenKey'], $child); } $children[$key] = $this->formatArgumentValue($child, ["format" => $options["childrenFormat"]]); } $argumentValue = $children; break; // Raw string value of the tag as value. Default. case VALUE_FORMAT_RAW: default: $argumentValue = $this->resolveValueString((string) $node); break; } return $argumentValue; } $xml = <<<EOL <config> <formatters> <fileformatter> <class>Monolog\Formatter\LineFormatter</class> <format>[%datetime%] %level_name%: %message%&#xA;</format> </fileformatter> <mongoformatter> <class>Bettr\Service\Log\Formatter\MongoDBFormatter</class> </mongoformatter> </formatters> <handlers> <filelog> <class>Monolog\Handler\StreamHandler</class> <level>DEBUG</level> <formatter>fileformatter</formatter> <processors>psr_processor</processors> <stream>../system/var/logs/debug.log</stream> </filelog> <mongolog> <class>Bettr\Service\Log\Handler\MongoServiceHandler</class> <level>ERROR</level> <formatter>mongoformatter</formatter> <mongoManager>{service:mongo}</mongoManager> <processors type="array">psr_processor</processors> <collection>logger</collection> </mongolog> </handlers> <processors> <psr_processor> <class>Monolog\Processor\PsrLogMessageProcessor</class> </psr_processor> </processors> <loggers> <bettr> <handlers type="array"> <handler>filelog</handler> <handler>mongolog</handler> </handlers> </bettr> </loggers> </config> EOL; $simpleXMLElement = new SimpleXMLElement($xml); var_dump(formatArgumentValue($simpleXMLElement, ["format" => VALUE_FORMAT_TAG_NAME_VALUE_ASSOC]));
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Fatal error: Uncaught Error: Using $this when not in object context in /in/ERLft:24 Stack trace: #0 /in/ERLft(124): formatArgumentValue(Object(SimpleXMLElement), Array) #1 {main} thrown in /in/ERLft on line 24
Process exited with code 255.
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 Fatal error: Uncaught Error: Using $this when not in object context in /in/ERLft:24 Stack trace: #0 /in/ERLft(124): formatArgumentValue(Object(SimpleXMLElement), Array) #1 {main} thrown in /in/ERLft on line 24
Process exited with code 255.
Output for 5.6.0 - 5.6.40
Fatal error: Using $this when not in object context in /in/ERLft on line 24
Process exited with code 255.

preferences:
227.78 ms | 401 KiB | 284 Q