{"id":12129,"date":"2022-05-17T10:00:00","date_gmt":"2022-05-17T01:00:00","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=12129"},"modified":"2022-05-17T10:01:15","modified_gmt":"2022-05-17T01:01:15","slug":"converting-xml-to-array-with-php","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/12129","title":{"rendered":"Converting XML to Array with PHP"},"content":{"rendered":"\n<p>Today, I would like to share about converting xml data to associated array in PHP. Let\u2019s take a look.<\/p>\n\n\n\n<p>We will need just 4 steps to perform that.<\/p>\n\n\n\n<p><strong>Step 1 : read the xml file and get file contents.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ read xml file\n$xmlFile = \"sample.xml\";\n$xmlFileContents = file_get_contents($xmlFile);<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Step 2 : convert xml data into xml object.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Convert XML Data into XML Object\n$xmlObject = simplexml_load_string($xmlFileContents);<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Step 3 : convert xml object to Associated array<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Convert XML data object to  Array\n$json  = json_encode($xmlObject);\n$array = json_decode($json, true);\t<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Step 4 : print the result<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ print array\nprint \"&lt;pre&gt;\";\nprint_r($array);\nprint \"&lt;\/pre&gt;\";<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>This is all for now. Hope you enjoy that.<\/p>\n\n\n\n<p>By Asahi<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/12129\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/12129\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Converting XML to Array with PHP\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/12129\" data-text=\"Converting XML to Array with PHP\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/12129\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/12129\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Today, I would like to share about converting xml data to associated array in PHP. Let\u2019s take a look. We will  [&hellip;]<\/p>\n","protected":false},"author":20,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/12129"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=12129"}],"version-history":[{"count":4,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/12129\/revisions"}],"predecessor-version":[{"id":12133,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/12129\/revisions\/12133"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=12129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=12129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=12129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}