{"id":1201,"date":"2013-08-06T01:07:04","date_gmt":"2013-08-06T01:07:04","guid":{"rendered":"http:\/\/www.phpmind.com\/blog\/?p=1201"},"modified":"2013-08-06T01:07:04","modified_gmt":"2013-08-06T01:07:04","slug":"how-to-convert-php-array-in-diffrent-kind-of-json-objects","status":"publish","type":"post","link":"https:\/\/www.phpmind.com\/blog\/2013\/08\/how-to-convert-php-array-in-diffrent-kind-of-json-objects\/","title":{"rendered":"How to convert PHP array in diffrent kind of json objects?"},"content":{"rendered":"<p>JSON objects are used everywhere and very easy to implement.<br \/>\nThanks to PHP it has inbuilt function but if you want different kind of json object which is possible based on requirement. In my case i wanted to use<br \/>\n var availableTags = [{&#8220;key&#8221;:1,&#8221;value&#8221;:&#8221;AERO 1&#8243;},{&#8220;key&#8221;:2,&#8221;value&#8221;:&#8221;AERO 3&#8243;},{&#8220;key&#8221;:3,&#8221;value&#8221;:&#8221;AIR CANADA 4&#8243;},{&#8220;key&#8221;:6,&#8221;value&#8221;:&#8221;AIR CANADA 26&#8243;},{&#8220;key&#8221;:7,&#8221;value&#8221;:&#8221;AIR CANADA JAZZzzz&#8221;}];<\/p>\n<p>if have key value pair of array json_encode($yourArray) will not return<br \/>\nabove format!  <\/p>\n<p>Well that was my requirement, yours could be different but here is the solution.<br \/>\nYou can tweak little bit and ready to go!<\/p>\n<pre lang=\"php\">\r\n<?Php\r\n$temAccNameArray = array();\r\nforeach($Acc_Name_List as $key => $value)\r\n{\r\n   if(empty($key) && empty($value))\r\n   {\r\n\t  continue;\r\n    }\r\n\t$temAccNameArray[] = array('key' => $key,'value'=>$value);\r\n}\r\necho json_encode($temAccNameArray);\r\n?>;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>JSON objects are used everywhere and very easy to implement. Thanks to PHP it has inbuilt function but if you want different kind of json object which is possible based on requirement. In my case i wanted to use var availableTags = [{&#8220;key&#8221;:1,&#8221;value&#8221;:&#8221;AERO 1&#8243;},{&#8220;key&#8221;:2,&#8221;value&#8221;:&#8221;AERO 3&#8243;},{&#8220;key&#8221;:3,&#8221;value&#8221;:&#8221;AIR CANADA 4&#8243;},{&#8220;key&#8221;:6,&#8221;value&#8221;:&#8221;AIR CANADA 26&#8243;},{&#8220;key&#8221;:7,&#8221;value&#8221;:&#8221;AIR CANADA JAZZzzz&#8221;}]; if have key value [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1201","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/comments?post=1201"}],"version-history":[{"count":1,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1201\/revisions"}],"predecessor-version":[{"id":1202,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1201\/revisions\/1202"}],"wp:attachment":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/media?parent=1201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/categories?post=1201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/tags?post=1201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}