{"id":8836,"date":"2020-06-08T11:00:54","date_gmt":"2020-06-08T02:00:54","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=8836"},"modified":"2020-06-05T19:46:18","modified_gmt":"2020-06-05T10:46:18","slug":"php-json","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/8836","title":{"rendered":"PHP Json"},"content":{"rendered":"\n<p>Today I will talk about how to operate Json data in PHP especially using <code>json_encode<\/code> &amp; <code>json_decode<\/code>.<\/p>\n\n\n\n<p>So, Lets get started. We have an array like this.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$months = array(\"Jan\"=>\"01\",\"Feb\"=>\"02\",\"March\"=>\"03\",\"April\"=>\"04\");\n?><\/code><\/pre>\n\n\n\n<p>If we want to change that array to json format, we can do like this.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$months = array(\"Jan\"=>\"01\",\"Feb\"=>\"02\",\"March\"=>\"03\",\"April\"=>\"04\");\n\necho json_encode($months);\n?><\/code><\/pre>\n\n\n\n<p>So What about decoding, decoding the also similar with encoding.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$json = '{\"Jan\":\"01\",\"Feb\":\"02\",\"March\":\"03\",\"April\":\"04\"}';\n\n\/\/we can treat the decoded data in two ways (object &amp; array)\n\/\/treating like an object.\n$obj = json_decode($json);\necho $obj->Jan;\n\n\/\/ treating like an array\n$array = json_decode($json,true);\necho $array['Jan'];\n?><\/code><\/pre>\n\n\n\n<p>The sample concept if you want to loop through the decoded json.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$json = '{\"Jan\":\"01\",\"Feb\":\"02\",\"March\":\"03\",\"April\":\"04\"}';\n\n$array = json_decode($jsonobj, true);\n\nforeach($array as $key => $value) {\n  echo $key . \" => \" . $value . \"&lt;br>\";\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$json = '{\"Jan\":\"01\",\"Feb\":\"02\",\"March\":\"03\",\"April\":\"04\"}';\n\n$obj = json_decode($jsonobj);\n\nforeach($obj as $key => $value) {\n  echo $key . \" => \" . $value . \"&lt;br>\";\n}<\/code><\/pre>\n\n\n\n<p>By Yuuma<\/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\/8836\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/8836\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"PHP Json\" 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\/8836\" data-text=\"PHP Json\" 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\/8836\" 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\/8836\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Today I will talk about how to operate Json data in PHP especially using json_encode &amp; json_decode. So, Le [&hellip;]<\/p>\n","protected":false},"author":18,"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\/8836"}],"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\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=8836"}],"version-history":[{"count":2,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/8836\/revisions"}],"predecessor-version":[{"id":8838,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/8836\/revisions\/8838"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=8836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=8836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=8836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}