<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: XML string to PHP array</title>
	<atom:link href="http://gaarf.info/2009/08/13/xml-string-to-php-array/feed/" rel="self" type="application/rss+xml" />
	<link>http://gaarf.info/2009/08/13/xml-string-to-php-array/</link>
	<description>my slice of the internets</description>
	<lastBuildDate>Thu, 24 Sep 2009 15:23:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: ad</title>
		<link>http://gaarf.info/2009/08/13/xml-string-to-php-array/comment-page-1/#comment-169662</link>
		<dc:creator>ad</dc:creator>
		<pubDate>Thu, 24 Sep 2009 15:23:26 +0000</pubDate>
		<guid isPermaLink="false">http://gaarf.info/?p=358#comment-169662</guid>
		<description>Umair, to build XML I suggest you have a look at DOMDocument class. In particular, have a look at http://www.php.net/manual/en/domdocument.savexml.php</description>
		<content:encoded><![CDATA[<p>Umair, to build XML I suggest you have a look at DOMDocument class. In particular, have a look at <a href="http://www.php.net/manual/en/domdocument.savexml.php" rel="nofollow">http://www.php.net/manual/en/domdocument.savexml.php</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Umair Jabbar</title>
		<link>http://gaarf.info/2009/08/13/xml-string-to-php-array/comment-page-1/#comment-169647</link>
		<dc:creator>Umair Jabbar</dc:creator>
		<pubDate>Thu, 24 Sep 2009 05:28:33 +0000</pubDate>
		<guid isPermaLink="false">http://gaarf.info/?p=358#comment-169647</guid>
		<description>hey there,
i actually need a snippet which can convert a php array to xml string,</description>
		<content:encoded><![CDATA[<p>hey there,<br />
i actually need a snippet which can convert a php array to xml string,</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rasmus</title>
		<link>http://gaarf.info/2009/08/13/xml-string-to-php-array/comment-page-1/#comment-168071</link>
		<dc:creator>Rasmus</dc:creator>
		<pubDate>Fri, 14 Aug 2009 14:59:00 +0000</pubDate>
		<guid isPermaLink="false">http://gaarf.info/?p=358#comment-168071</guid>
		<description>Well, that was a bit ugly.  Here is a nicer version: http://codepad.org/5CrZpqeh</description>
		<content:encoded><![CDATA[<p>Well, that was a bit ugly.  Here is a nicer version: <a href="http://codepad.org/5CrZpqeh" rel="nofollow">http://codepad.org/5CrZpqeh</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rasmus</title>
		<link>http://gaarf.info/2009/08/13/xml-string-to-php-array/comment-page-1/#comment-168070</link>
		<dc:creator>Rasmus</dc:creator>
		<pubDate>Fri, 14 Aug 2009 14:56:55 +0000</pubDate>
		<guid isPermaLink="false">http://gaarf.info/?p=358#comment-168070</guid>
		<description>Last time someone asked me how to do this, this was my answer.  The benefits here are that it handles xml namespaces nicely:

function xmlToArray($xml,$ns=null){
  $a = array();
  for($xml-&gt;rewind(); $xml-&gt;valid(); $xml-&gt;next()) {
    $key = $xml-&gt;key();
    if(!isset($a[$key])) { $a[$key] = array(); $i=0; }
    else $i = count($a[$key]);
    $simple = true;
    foreach($xml-&gt;current()-&gt;attributes() as $k=&gt;$v) {
        $a[$key][$i][$k]=(string)$v;
        $simple = false;
    }
    if($ns) foreach($ns as $nid=&gt;$name) {
      foreach($xml-&gt;current()-&gt;attributes($name) as $k=&gt;$v) {
         $a[$key][$i][$nid.&#039;:&#039;.$k]=(string)$v;
         $simple = false;
      }
    } 
    if($xml-&gt;hasChildren()) {
        if($simple) $a[$key][$i] = xmlToArray($xml-&gt;current(), $ns);
        else $a[$key][$i][&#039;content&#039;] = xmlToArray($xml-&gt;current(), $ns);
    } else {
        if($simple) $a[$key][$i] = strval($xml-&gt;current());
        else $a[$key][$i][&#039;content&#039;] = strval($xml-&gt;current());
    }
    $i++;
  }
  return $a;
}

$xml = new SimpleXmlIterator(&#039;./a.xml&#039;, null, true);
$namespaces = $xml-&gt;getNamespaces(true);
$arr = xmlToArray($xml,$namespaces);</description>
		<content:encoded><![CDATA[<p>Last time someone asked me how to do this, this was my answer.  The benefits here are that it handles xml namespaces nicely:</p>
<p>function xmlToArray($xml,$ns=null){<br />
  $a = array();<br />
  for($xml-&gt;rewind(); $xml-&gt;valid(); $xml-&gt;next()) {<br />
    $key = $xml-&gt;key();<br />
    if(!isset($a[$key])) { $a[$key] = array(); $i=0; }<br />
    else $i = count($a[$key]);<br />
    $simple = true;<br />
    foreach($xml-&gt;current()-&gt;attributes() as $k=&gt;$v) {<br />
        $a[$key][$i][$k]=(string)$v;<br />
        $simple = false;<br />
    }<br />
    if($ns) foreach($ns as $nid=&gt;$name) {<br />
      foreach($xml-&gt;current()-&gt;attributes($name) as $k=&gt;$v) {<br />
         $a[$key][$i][$nid.':'.$k]=(string)$v;<br />
         $simple = false;<br />
      }<br />
    }<br />
    if($xml-&gt;hasChildren()) {<br />
        if($simple) $a[$key][$i] = xmlToArray($xml-&gt;current(), $ns);<br />
        else $a[$key][$i]['content'] = xmlToArray($xml-&gt;current(), $ns);<br />
    } else {<br />
        if($simple) $a[$key][$i] = strval($xml-&gt;current());<br />
        else $a[$key][$i]['content'] = strval($xml-&gt;current());<br />
    }<br />
    $i++;<br />
  }<br />
  return $a;<br />
}</p>
<p>$xml = new SimpleXmlIterator(&#8216;./a.xml&#8217;, null, true);<br />
$namespaces = $xml-&gt;getNamespaces(true);<br />
$arr = xmlToArray($xml,$namespaces);</p>
]]></content:encoded>
	</item>
</channel>
</rss>
