如何在PHP中将std对象转换为simpleXMLelement对象

时间:2016-02-02 09:25:03

标签: php xml

在PHP中将stdClass对象转换为SimpleXMLElement对象的最佳方法是什么?

我想将stdClass对象转换为simpleXMLElement对象。屏幕截图已附上。 我使用以下代码按类别分隔产品,但它不起作用。

<?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

0 个答案:

没有答案
相关问题