{"id":62,"date":"2010-02-10T18:32:13","date_gmt":"2010-02-10T23:32:13","guid":{"rendered":"http:\/\/jamesroberts.name\/blog\/?p=62"},"modified":"2012-10-10T11:03:04","modified_gmt":"2012-10-10T16:03:04","slug":"simplexml-to-array","status":"publish","type":"post","link":"https:\/\/jamesroberts.name\/blog\/2010\/02\/10\/simplexml-to-array\/","title":{"rendered":"simplexml to array"},"content":{"rendered":"<p>Sometimes dealing with PHP&#8217;s simplexml object can be annoying, and you just wish it was a simple array.  There are a ton of functions out there for that, but I&#8217;ve found this one works best =)<\/p>\n<p>Source: <a href=\"http:\/\/www.php.net\/manual\/en\/ref.simplexml.php\">http:\/\/www.php.net\/manual\/en\/ref.simplexml.php<\/a><\/p>\n<pre lang=\"php\">\r\nfunction simplexml2array($xml) {\r\n\tif (get_class($xml) == 'SimpleXMLElement') {\r\n\t\t$attributes = $xml->attributes();\r\n\t\tforeach($attributes as $k=>$v) {\r\n\t\t\tif ($v) $a[$k] = (string) $v;\r\n\t\t}\r\n\t\t$x = $xml;\r\n\t\t$xml = get_object_vars($xml);\r\n\t}\r\n\tif (is_array($xml)) {\r\n\t\tif (count($xml) == 0) return (string) $x; \/\/ for CDATA\r\n\t\tforeach($xml as $key=>$value) {\r\n\t\t\t$r[$key] = $this->simplexml2array($value);\r\n\t\t}\r\n\t\tif (isset($a)) $r['@attributes'] = $a;    \/\/ Attributes\r\n\t\treturn $r;\r\n\t}\r\n\treturn (string) $xml;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes dealing with PHP&#8217;s simplexml object can be annoying, and you just wish it was a simple array. There are a ton of functions out there for that, but I&#8217;ve found this one works best =) Source: http:\/\/www.php.net\/manual\/en\/ref.simplexml.php function simplexml2array($xml) { if (get_class($xml) == &#8216;SimpleXMLElement&#8217;) { $attributes = $xml->attributes(); foreach($attributes as $k=>$v) { if ($v) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[17,62,16],"_links":{"self":[{"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/posts\/62"}],"collection":[{"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/comments?post=62"}],"version-history":[{"count":5,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"predecessor-version":[{"id":221,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/posts\/62\/revisions\/221"}],"wp:attachment":[{"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}