Jump to content

object-to-array function works but throws php warning


stevieontario
Go to solution Solved by requinix,

Recommended Posts

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?

Link to comment
Share on other sites

  • Solution

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 by requinix
  • Like 2
Link to comment
Share on other sites

Dark Administrator (aka requinix :happy-04: ):

 

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 by cyberRobot
add requinix
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.