0

What is the best way to convert stdClass Object into SimpleXMLElement Object in PHP?

I want to convert stdClass object to simpleXMLElement Object. Screen shot is attached. I am using the following code to separate products by categories but it is not working.

<?php
  $xml=simplexml_load_file("./files/downloaded.xml");
  for($x=0;$x < count($xml->product); $x++)
  {
      if( preg_match("#Groceries > ([ a-zA-Z0-9]+) >#i",$xml->product[$x]->category,$match) )
      {
        $match[1] = str_replace(" ", "" , strtolower($match[1]) );
        if($match[1] == "baby"){
            $baby->product[$x] = $xml->product[$x];
        }

      }
  }
  print "<pre>";
  print_r($baby);
  print "</pre>";

  // Save XML file does not work
  file_put_contents("./files/baby.xml",$baby);
  print "Baby.xml file has been created";
?>

SimpleXMLElement Object stdClass Object

2
  • 4
    Please do not an image of your code. Copy the code into the question.
    – DerApe
    Feb 2, 2016 at 9:31
  • @derape thanks for your comment dear, I have just edited the question with code. can you help me now ?
    – Abid Ali
    Feb 2, 2016 at 9:46

0

Browse other questions tagged or ask your own question.