3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $xml = '<root><case1><name> </name></case1><case2><name></name></case2><case3><name>ukits</name></case3></root>'; libxml_use_internal_errors(true); $content = simplexml_load_string($xml); if ($content === false) { libxml_clear_errors(); } print_r($content); $content = objectToArray($content); print_r($content); function objectToArray($obj) { if (is_object($obj)) { $obj = (array) $obj; } if (count($obj) == 0) { $obj = ''; } if (is_array($obj)) { $new = array(); foreach ($obj as $key => $val) { $new[$key] = objectToArray($val); } } else { $new = $obj; } return $new; }
based on Z0ape
Output for 7.2.0 - 7.2.6
SimpleXMLElement Object ( [case1] => SimpleXMLElement Object ( [name] => SimpleXMLElement Object ( [0] => ) ) [case2] => SimpleXMLElement Object ( [name] => SimpleXMLElement Object ( ) ) [case3] => SimpleXMLElement Object ( [name] => ukits ) ) Warning: count(): Parameter must be an array or an object that implements Countable in /in/SvI3B on line 21 Warning: count(): Parameter must be an array or an object that implements Countable in /in/SvI3B on line 21 Array ( [case1] => Array ( [name] => Array ( [0] => ) ) [case2] => Array ( [name] => ) [case3] => Array ( [name] => ukits ) )
Output for 5.3.20 - 5.3.29, 5.5.27 - 5.5.35, 5.6.11 - 7.1.20
SimpleXMLElement Object ( [case1] => SimpleXMLElement Object ( [name] => SimpleXMLElement Object ( [0] => ) ) [case2] => SimpleXMLElement Object ( [name] => SimpleXMLElement Object ( ) ) [case3] => SimpleXMLElement Object ( [name] => ukits ) ) Array ( [case1] => Array ( [name] => Array ( [0] => ) ) [case2] => Array ( [name] => ) [case3] => Array ( [name] => ukits ) )
Output for 5.4.10 - 5.5.26, 5.6.8 - 5.6.10
SimpleXMLElement Object ( [case1] => SimpleXMLElement Object ( [name] => SimpleXMLElement Object ( [0] => ) ) [case2] => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) ) [case3] => SimpleXMLElement Object ( [name] => ukits ) ) Array ( [case1] => Array ( [name] => Array ( [0] => ) ) [case2] => Array ( [0] => ) [case3] => Array ( [name] => ukits ) )