<?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>here be my blog</description>
	<lastBuildDate>Thu, 15 Dec 2011 16:28:16 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: ad</title>
		<link>http://gaarf.info/2009/08/13/xml-string-to-php-array/comment-page-1/#comment-193822</link>
		<dc:creator>ad</dc:creator>
		<pubDate>Mon, 12 Dec 2011 23:06:18 +0000</pubDate>
		<guid isPermaLink="false">http://gaarf.info/?p=358#comment-193822</guid>
		<description>@Ben: could you make a patch and submit a pull request to https://github.com/gaarf/XML-string-to-PHP-array ?</description>
		<content:encoded><![CDATA[<p>@Ben: could you make a patch and submit a pull request to <a href="https://github.com/gaarf/XML-string-to-PHP-array" rel="nofollow">https://github.com/gaarf/XML-string-to-PHP-array</a> ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://gaarf.info/2009/08/13/xml-string-to-php-array/comment-page-1/#comment-193818</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Mon, 12 Dec 2011 22:52:13 +0000</pubDate>
		<guid isPermaLink="false">http://gaarf.info/?p=358#comment-193818</guid>
		<description>One slight mod (added code) to the github.com code repository fixes both the &quot;empty to array&quot; issue and the &quot;non printing zeros issue&quot; for me (swiping @Jonathan&#039;s code and modifying it slightly):

From the clean code at github.com above the line &quot;$output[$t][] = $v;&quot; put the following :

&lt;code&gt;
          if(empty($v) &amp;&amp; $v !== &#039;0&#039;) {
           $v = &quot;&quot;;
          }
&lt;/code&gt;

This enables the zeros to show up in the array, otherwise zeros in the xml like &lt;code&gt;0&lt;/code&gt; show up as empty.

Again, fantastic code. The best XML to Array PHP function out there, hands down. This code should be polished/vetted and incorporated into the PHP core functions at a lower level, really. Would save a lot of people a lot of frustration.</description>
		<content:encoded><![CDATA[<p>One slight mod (added code) to the github.com code repository fixes both the &#8220;empty to array&#8221; issue and the &#8220;non printing zeros issue&#8221; for me (swiping @Jonathan&#8217;s code and modifying it slightly):</p>
<p>From the clean code at github.com above the line &#8220;$output[$t][] = $v;&#8221; put the following :</p>
<p><code><br />
          if(empty($v) &amp;&amp; $v !== '0') {<br />
           $v = "";<br />
          }<br />
</code></p>
<p>This enables the zeros to show up in the array, otherwise zeros in the xml like <code>0</code> show up as empty.</p>
<p>Again, fantastic code. The best XML to Array PHP function out there, hands down. This code should be polished/vetted and incorporated into the PHP core functions at a lower level, really. Would save a lot of people a lot of frustration.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kontur</title>
		<link>http://gaarf.info/2009/08/13/xml-string-to-php-array/comment-page-1/#comment-187641</link>
		<dc:creator>kontur</dc:creator>
		<pubDate>Thu, 17 Nov 2011 11:12:58 +0000</pubDate>
		<guid isPermaLink="false">http://gaarf.info/?p=358#comment-187641</guid>
		<description>Thanks for posting this code. I was struggling with php&#039;s own simpleXML, trying to wrap my head around how to deal with different namepspaces. Your function, however, was all sufficient to get two simple fields from the xml!

Thanks also to Aurelien for the fix on the empty string issue. ;)</description>
		<content:encoded><![CDATA[<p>Thanks for posting this code. I was struggling with php&#8217;s own simpleXML, trying to wrap my head around how to deal with different namepspaces. Your function, however, was all sufficient to get two simple fields from the xml!</p>
<p>Thanks also to Aurelien for the fix on the empty string issue. <img src='http://gaarf.info/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: PHP Converting XML to array &#124; SeekPHP.com</title>
		<link>http://gaarf.info/2009/08/13/xml-string-to-php-array/comment-page-1/#comment-187351</link>
		<dc:creator>PHP Converting XML to array &#124; SeekPHP.com</dc:creator>
		<pubDate>Thu, 10 Nov 2011 04:01:39 +0000</pubDate>
		<guid isPermaLink="false">http://gaarf.info/?p=358#comment-187351</guid>
		<description>[...] http://gaarf.info/2009/08/13/xml-string-to-php-array/ [...]</description>
		<content:encoded><![CDATA[<p>[...] <a href="http://gaarf.info/2009/08/13/xml-string-to-php-array/" rel="nofollow">http://gaarf.info/2009/08/13/xml-string-to-php-array/</a> [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jonathan</title>
		<link>http://gaarf.info/2009/08/13/xml-string-to-php-array/comment-page-1/#comment-186887</link>
		<dc:creator>Jonathan</dc:creator>
		<pubDate>Tue, 25 Oct 2011 14:54:34 +0000</pubDate>
		<guid isPermaLink="false">http://gaarf.info/?p=358#comment-186887</guid>
		<description>Given: &lt;code&gt;&lt;xml&gt;&lt;empty&gt;&lt;/empty&gt;&lt;zero&gt;0&lt;/zero&gt;&lt;/xml&gt;&lt;/code&gt;
Expected: &lt;code&gt;array(&quot;empty&quot; =&gt; &quot;&quot;, &quot;zero&quot; =&gt; &quot;0&quot; )&lt;/code&gt;
Returned: &lt;code&gt;array(&quot;empty&quot; =&gt; array(), &quot;zero&quot; =&gt; array()  )&lt;/code&gt;

Aurelien&#039;s comment above fixed the empty node issue. I added a line to fix the zero issue.

&lt;code&gt;if (isset($child-&gt;tagName)) {
&#160;$t = $child-&gt;tagName;
&#160;if (!isset($output[$t])) {
&#160;&#160;$output[$t] = array();
&#160;}
&#160;if(empty($v)) {
&#160;&#160;$v = &quot;&quot;;
&#160;}
&#160;$output[$t][] = $v;
} elseif ($v &#124;&#124; $v === &quot;0&quot;) {
&#160;$output = (string) $v;
}&lt;/code&gt;

Awesome otherwise. Thanks for the function</description>
		<content:encoded><![CDATA[<p>Given: <code>&lt;xml&gt;&lt;empty&gt;&lt;/empty&gt;&lt;zero&gt;0&lt;/zero&gt;&lt;/xml&gt;</code><br />
Expected: <code>array("empty" =&gt; "", "zero" =&gt; "0" )</code><br />
Returned: <code>array("empty" =&gt; array(), "zero" =&gt; array()  )</code></p>
<p>Aurelien&#8217;s comment above fixed the empty node issue. I added a line to fix the zero issue.</p>
<p><code>if (isset($child-&gt;tagName)) {<br />
&nbsp;$t = $child-&gt;tagName;<br />
&nbsp;if (!isset($output[$t])) {<br />
&nbsp;&nbsp;$output[$t] = array();<br />
&nbsp;}<br />
&nbsp;if(empty($v)) {<br />
&nbsp;&nbsp;$v = "";<br />
&nbsp;}<br />
&nbsp;$output[$t][] = $v;<br />
} elseif ($v || $v === "0") {<br />
&nbsp;$output = (string) $v;<br />
}</code></p>
<p>Awesome otherwise. Thanks for the function</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://gaarf.info/2009/08/13/xml-string-to-php-array/comment-page-1/#comment-186659</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Mon, 17 Oct 2011 00:25:07 +0000</pubDate>
		<guid isPermaLink="false">http://gaarf.info/?p=358#comment-186659</guid>
		<description>Seriousf**kingly!!!!!!!!!! xmlstr_to_array() is the ONLY script that I found to work to convert XML to an array in PHP. I had search for about two weeks and sifted through like 20 functions/classes and to be honest most were all shit. 

Look man, I have no idea why this is such a flipping major issue in PHP, but seriously your code gets a five star rating. And by the way, you should remove Rasmus&#039;s xmlToArray() function because it just don&#039;t work out of the box.

I&#039;ll repeat myself. Why is converting XML to an array so hard in PHP and why are there 1500 bunny trails to broken code to accomplish this (what should apparently be a) simple task? Many of them are written in such a way that they quickly exhaust sever memory...your&#039;s seems to hold up very well under a severe XML beating.

Anyways, thank you very much! I wish I would have found this two years ago. It should be easier to find! For me, now it is as a part of my permanent library, bundled with Curl as a viable SOAP tool.

Ben

&lt;code&gt;
// My real world PHP Curl SOAP solution utilizing xmlstr_to_array() :

function curl_soap($xml,$api_url,$soap_action,$timeout=60) {
	$user_agent = &quot;Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)&quot;;
	
	$ch = curl_init(); 
	curl_setopt($ch, CURLOPT_URL, $api_url ); // set url to post to
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return variable 
	curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // connection timeout
	curl_setopt($ch, CURLOPT_POSTFIELDS, $xml ); 
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);	
	
	$header[] = &quot;SOAPAction: &quot;. $soap_action ;
	$header[] = &quot;MIME-Version: 1.0&quot;;
	$header[] = &quot;Content-type: text/xml; charset=utf-8&quot;;
	
	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	$xml = curl_exec($ch); // run the whole process
	
	if (curl_errno($ch)) {
		$error[&#039;curl_error&#039;] = &#039;Connection Error : &#039;. curl_error($ch) ; 
		return $error ; 
	} else {
		curl_close($ch);		
		$arr = xmlstr_to_array($xml);
		return $arr ;
	}
}
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Seriousf**kingly!!!!!!!!!! xmlstr_to_array() is the ONLY script that I found to work to convert XML to an array in PHP. I had search for about two weeks and sifted through like 20 functions/classes and to be honest most were all shit. </p>
<p>Look man, I have no idea why this is such a flipping major issue in PHP, but seriously your code gets a five star rating. And by the way, you should remove Rasmus&#8217;s xmlToArray() function because it just don&#8217;t work out of the box.</p>
<p>I&#8217;ll repeat myself. Why is converting XML to an array so hard in PHP and why are there 1500 bunny trails to broken code to accomplish this (what should apparently be a) simple task? Many of them are written in such a way that they quickly exhaust sever memory&#8230;your&#8217;s seems to hold up very well under a severe XML beating.</p>
<p>Anyways, thank you very much! I wish I would have found this two years ago. It should be easier to find! For me, now it is as a part of my permanent library, bundled with Curl as a viable SOAP tool.</p>
<p>Ben</p>
<p><code><br />
// My real world PHP Curl SOAP solution utilizing xmlstr_to_array() :</p>
<p>function curl_soap($xml,$api_url,$soap_action,$timeout=60) {<br />
	$user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";</p>
<p>	$ch = curl_init();<br />
	curl_setopt($ch, CURLOPT_URL, $api_url ); // set url to post to<br />
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return variable<br />
	curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // connection timeout<br />
	curl_setopt($ch, CURLOPT_POSTFIELDS, $xml );<br />
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);<br />
	curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);	</p>
<p>	$header[] = "SOAPAction: ". $soap_action ;<br />
	$header[] = "MIME-Version: 1.0";<br />
	$header[] = "Content-type: text/xml; charset=utf-8";</p>
<p>	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);<br />
	$xml = curl_exec($ch); // run the whole process</p>
<p>	if (curl_errno($ch)) {<br />
		$error['curl_error'] = 'Connection Error : '. curl_error($ch) ;<br />
		return $error ;<br />
	} else {<br />
		curl_close($ch);<br />
		$arr = xmlstr_to_array($xml);<br />
		return $arr ;<br />
	}<br />
}<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nabeel Khan</title>
		<link>http://gaarf.info/2009/08/13/xml-string-to-php-array/comment-page-1/#comment-186506</link>
		<dc:creator>Nabeel Khan</dc:creator>
		<pubDate>Wed, 12 Oct 2011 00:13:59 +0000</pubDate>
		<guid isPermaLink="false">http://gaarf.info/?p=358#comment-186506</guid>
		<description>Thanks man! your first piece of code worked for my current situation though! 

I&#039;ve made a post regarding this (stealing some of your code) but gave you a backlink too for further stuff! :)</description>
		<content:encoded><![CDATA[<p>Thanks man! your first piece of code worked for my current situation though! </p>
<p>I&#8217;ve made a post regarding this (stealing some of your code) but gave you a backlink too for further stuff! <img src='http://gaarf.info/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Convert XML string to PHP Array</title>
		<link>http://gaarf.info/2009/08/13/xml-string-to-php-array/comment-page-1/#comment-186505</link>
		<dc:creator>Convert XML string to PHP Array</dc:creator>
		<pubDate>Wed, 12 Oct 2011 00:05:53 +0000</pubDate>
		<guid isPermaLink="false">http://gaarf.info/?p=358#comment-186505</guid>
		<description>[...] result in an empty array position at that spot. This can be fixed by following code provided here: http://gaarf.info/2009/08/13/xml-string-to-php-array/Related PostsDo you license your scripts before giving it to the client?WordPress Show image title [...]</description>
		<content:encoded><![CDATA[<p>[...] result in an empty array position at that spot. This can be fixed by following code provided here: http://gaarf.info/2009/08/13/xml-string-to-php-array/Related PostsDo you license your scripts before giving it to the client?WordPress Show image title [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aurelien</title>
		<link>http://gaarf.info/2009/08/13/xml-string-to-php-array/comment-page-1/#comment-184749</link>
		<dc:creator>Aurelien</dc:creator>
		<pubDate>Mon, 01 Aug 2011 11:37:19 +0000</pubDate>
		<guid isPermaLink="false">http://gaarf.info/?p=358#comment-184749</guid>
		<description>That was very useful, thanks!
I had some empty fields and they appeared as an empty array instead of an empty string, so I added this line: &lt;code&gt;if(empty($v)) $v = &#039;&#039;;&lt;/code&gt;

Here:
&lt;code&gt;	if(isset($child-&gt;tagName)) {
&#160;&#160;&#160;$t = $child-&gt;tagName;
&#160;&#160;&#160;if(!isset($output[$t])) {
&#160;&#160;&#160;&#160;&#160;&#160;$output[$t] = array();
&#160;&#160;&#160;}
&#160;&#160;&#160;&lt;strong&gt;		if(empty($v)) $v = &#039;&#039;;&lt;/strong&gt;
&#160;&#160;&#160;$output[$t][] = $v;
	}&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>That was very useful, thanks!<br />
I had some empty fields and they appeared as an empty array instead of an empty string, so I added this line: <code>if(empty($v)) $v = '';</code></p>
<p>Here:<br />
<code>	if(isset($child-&gt;tagName)) {<br />
&nbsp;&nbsp;&nbsp;$t = $child-&gt;tagName;<br />
&nbsp;&nbsp;&nbsp;if(!isset($output[$t])) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$output[$t] = array();<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;<strong>		if(empty($v)) $v = '';</strong><br />
&nbsp;&nbsp;&nbsp;$output[$t][] = $v;<br />
	}</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ISM Dhanbad Social Network</title>
		<link>http://gaarf.info/2009/08/13/xml-string-to-php-array/comment-page-1/#comment-184287</link>
		<dc:creator>ISM Dhanbad Social Network</dc:creator>
		<pubDate>Wed, 13 Jul 2011 20:08:19 +0000</pubDate>
		<guid isPermaLink="false">http://gaarf.info/?p=358#comment-184287</guid>
		<description>Thank you very much...
I was searching for same kind of xml to array. I created my self bu some problem was occurring, and that was solved by your post..</description>
		<content:encoded><![CDATA[<p>Thank you very much&#8230;<br />
I was searching for same kind of xml to array. I created my self bu some problem was occurring, and that was solved by your post..</p>
]]></content:encoded>
	</item>
</channel>
</rss>

