stevieontario Posted September 7, 2016 Share Posted September 7, 2016 I want to use a custom function called object2array to turn an xml string into an array. The function works, but gives this warning: Warning: get_object_vars() expects parameter 1 to be object, string given in <program.php>. Here is the function with the call to get_object_vars: function object2array($object) { $return = NULL; if(is_array($object)) { foreach($object as $key => $value) $return[$key] = object2array($value); } else { $var = get_object_vars($object); if($var) { foreach($var as $key => $value) $return[$key] = object2array($value); } else return strval($object); } return $return; } Here is my xml string, which is in the file file.xml: <?xml version="1.0" encoding="UTF-8"?><resultset> <row> <name>Happy</name> <age>20</age> </row> <row> <name>Harry</name> <age>25</age> </row> </resultset> ... and here is where I call the function: foreach(glob("/path_to_file/*.xml") as $filename) { $xmlname = basename($filename); $xml_file = simplexml_load_file($filename) or die("no data loaded"); } $xml_file = object2array($xml_file); echo '<pre>'; print_r($xml_file); echo '</pre>'; It's confusing to me, because print_r($xml_file) says it is an object. Obviously it's an issue with the get_object_vars call in object2array, or with its scope in the function. But I can't figure it out. Any insight into why this is happening and how I could get the function to work without throwing the warning? Quote Link to comment https://forums.phpfreaks.com/topic/302095-object-to-array-function-works-but-throws-php-warning/ Share on other sites More sharing options...
Solution requinix Posted September 7, 2016 Solution Share Posted September 7, 2016 (edited) Really tired of people wanting arrays over objects... Look. $xml = new SimpleXMLElement("/path_to_file/file.xml", 0, true); // or simplexml_load_file if you insist foreach ($xml->row as $row) { echo "{$row->name} is {$row->age} years old<br>\n"; } See how easy that was? As for the actual problem, try it out on a regular array (which has the same problem that objects do). echo "<pre>"; print_r(object2array(array("name" => "Happy", "age" => 20))); echo "</pre>";Now try to understand what it is doing:1. Is $object an array? Yes. foreach over it and call object2array on each member. 2. Is $object["name"] an array? No. Call get_object_vars on it, see that the return value is no good, and call strval. 3. Is $object["age"] an array? No. Call get_object_vars on it, see that the return value is no good, and call strval. It's assuming that every single value nested within the $object is either an object or array. And that's not necessarily - or even likely to be - true. It needs to check a) if $object is an array, or b) if $object is an object, or otherwise c) it's neither. But don't do that. Just use SimpleXML like it's supposed to be used. Objects aren't scary. They won't bite. Edited September 7, 2016 by requinix 2 Quote Link to comment https://forums.phpfreaks.com/topic/302095-object-to-array-function-works-but-throws-php-warning/#findComment-1537138 Share on other sites More sharing options...
stevieontario Posted September 7, 2016 Author Share Posted September 7, 2016 (edited) Dark Administrator (aka requinix ): Good grief. Can't believe I was approaching the problem like that -- scared of objects, exactly as you say. I had wondered why simplexml is called simplexml. It's simple. Yes, your first example was incredibly easy. Thanks! Edited September 7, 2016 by cyberRobot add requinix Quote Link to comment https://forums.phpfreaks.com/topic/302095-object-to-array-function-works-but-throws-php-warning/#findComment-1537139 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.