{"id":129144,"date":"2022-10-22T16:26:36","date_gmt":"2022-10-22T16:26:36","guid":{"rendered":"https:\/\/phppot.com\/?p=19798"},"modified":"2022-10-22T16:26:36","modified_gmt":"2022-10-22T16:26:36","slug":"php-array-to-json-string-convert-with-online-demo","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/10\/22\/php-array-to-json-string-convert-with-online-demo\/","title":{"rendered":"PHP Array to JSON String Convert with Online Demo"},"content":{"rendered":"<div class=\"modified-on\" readability=\"7.1304347826087\"> by <a href=\"https:\/\/phppot.com\/about\/\">Vincy<\/a>. Last modified on October 22nd, 2022.<\/div>\n<p>JSON is the best format to transfer data over network. It is an easily parsable format comparatively. That\u2019s why most of the API accepts parameters and returns responses in JSON.<\/p>\n<p>There are online tools to convert an array to a JSON object. This tutorial teaches how to create a program to convert various types of PHP array input into a JSON format.<\/p>\n<p>It has 4 different examples for converting a PHP array to JSON. Those are too tiny in purpose to let beginners understand this concept easily.<\/p>\n<h2>Quick example<\/h2>\n<p>This quick example is simply coded with a three-line straightforward solution. It takes a single-dimensional PHP array and converts it to JSON.<\/p>\n<div class=\"post-section-highlight\" readability=\"35\">\n<pre class=\"prettyprint\"><code class=\"language-php\">&lt;?php\n$array = array(100, 250, 375, 400);\n$jsonString = json_encode($array);\necho $jsonString;\n?&gt;\n<\/code><\/pre>\n<\/div>\n<p><a class=\"demo\" href=\"https:\/\/phppot.com\/demo\/php-array-to-json\/\">View Demo<\/a><\/p>\n<p>The other different array-to-JSON examples handle simple to complex array conversion. It also applies pre-modification (like array mapping) before conversion. The four examples are,<\/p>\n<ol>\n<li>Simple to complex PHP array to JSON.<\/li>\n<li>Remove array keys before converting to JSON.<\/li>\n<li>Convert PHP array with accented characters to JSON<\/li>\n<li>PHP Array to JSON with pretty-printing<\/li>\n<\/ol>\n<p>If you want the code for the reverse to <a href=\"https:\/\/phppot.com\/php\/php-object-to-array\/\">decode JSON objects to an array<\/a>, then the linked article has examples.<\/p>\n<p>See this online demo to convert an array of comma-separated values into a JSON object.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-19834\" src=\"https:\/\/phppot.com\/wp-content\/uploads\/2022\/10\/php-array-to-json-550x296.jpg\" alt=\"php array to json\" width=\"550\" height=\"296\" srcset=\"https:\/\/phppot.com\/wp-content\/uploads\/2022\/10\/php-array-to-json-550x296.jpg 550w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/10\/php-array-to-json-300x162.jpg 300w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/10\/php-array-to-json-768x414.jpg 768w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/10\/php-array-to-json.jpg 960w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\"><\/p>\n<h2 class=\"p1\">1) Simple to complex PHP array to JSON<\/h2>\n<p>This code handles 3 types of array data into a JSON object. In PHP, it is very easy to convert an array to JSON.<\/p>\n<p>It is a one-line code by using the <a href=\"https:\/\/phppot.com\/php\/php-json-encode-and-decode\/\">PHP json_encode()<\/a> function.<\/p>\n<pre class=\"prettyprint\"><code class=\"language-php\">&lt;?php\n\/\/ PHP Array to JSON string conversion for\n\/\/ simple, associative and multidimensional arrays\n\/\/ all works the same way using json_encode\n\/\/ just present different arrays for example purposes only \/\/ simple PHP Array to JSON string\necho '&lt;h1&gt;PHP Array to JSON&lt;\/h1&gt;';\n$array = array( 100, 250, 375, 400\n);\n$jsonString = json_encode($array);\necho $jsonString; \/\/ Associative Array to JSON\necho '&lt;h2&gt;Associative PHP Array to JSON&lt;\/h2&gt;';\n$array = array( 'e1' =&gt; 1000, 'e2' =&gt; 1500, 'e3' =&gt; 2000, 'e4' =&gt; 2350, 'e5' =&gt; 3000\n);\n$jsonString = json_encode($array);\necho $jsonString; \/\/ multidimensional PHP Array to JSON string\necho '&lt;h2&gt;Multidimensional PHP Array to JSON&lt;\/h2&gt;';\n$multiArray = array( 'a1' =&gt; array( 'item_id' =&gt; 1, 'name' =&gt; 'Lion', 'type' =&gt; 'Wild', 'location' =&gt; 'Zoo' ), 'a2' =&gt; array( 'item_id' =&gt; 2, 'name' =&gt; 'Cat', 'type' =&gt; 'Domestic', 'location' =&gt; 'Home' )\n);\necho json_encode($multiArray);\n?&gt;\n<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"prettyprint\"><code>\/\/PHP Array to JSON\n[100,250,375,400] \/\/Associative PHP Array to JSON\n{\"e1\":1000,\"e2\":1500,\"e3\":2000,\"e4\":2350,\"e5\":3000} \/\/Multidimensional PHP Array to JSON\n{\"a1\":{\"item_id\":1,\"name\":\"Lion\",\"type\":\"Wild\",\"location\":\"Zoo\"},\"a2\":{\"item_id\":2,\"name\":\"Cat\",\"type\":\"Domestic\",\"location\":\"Home\"}}\n<\/code><\/pre>\n<h2 class=\"p1\">2) Remove array keys before converting to JSON<\/h2>\n<p>This code handles a different scenario of JSON conversion which must be helpful if needed. For example, if the array associates <em>subject=&gt;marks<\/em> and the user needs only the marks to plot it in a graph.<\/p>\n<p>It removes the user-defined keys from an associative array and applies <a href=\"https:\/\/www.php.net\/manual\/en\/function.json-encode.php\" target=\"_blank\" rel=\"noopener\">json_encode<\/a> to convert it. It is a two-step process.<\/p>\n<ol>\n<li>It applies PHP array_values() to read the value array.<\/li>\n<li>Then, it applies json_encode on the values array.<\/li>\n<\/ol>\n<pre class=\"prettyprint\"><code class=\"language-php\">&lt;?php\n\/\/ array_values() to remove assigned keys and convert to the original PHP Array key\necho '&lt;h1&gt;To remove assigned associative keys and PHP Array to JSON&lt;\/h1&gt;';\n$array = array( 'e1' =&gt; 1000, 'e2' =&gt; 1500, 'e3' =&gt; 2000, 'e4' =&gt; 2350, 'e5' =&gt; 3000\n); $jsonString = json_encode(array_values($array));\necho $jsonString;\n?&gt;\n<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"prettyprint\"><code>[1000,1500,2000,2350,3000]\n<\/code><\/pre>\n<h2 class=\"p1\">3) Convert the PHP array with accented characters to JSON<\/h2>\n<p>It is also a two-step process to convert the array of data containing accented characters.<\/p>\n<p>It applies UTF8 encoding on the array values before converting them into a JSON object.<\/p>\n<p>For encoding all the elements of the given array, it maps the utf8_encode() as a callback using the PHP array_map() function.<\/p>\n<p>We have seen <a href=\"https:\/\/phppot.com\/php\/power-of-php-arrays\/\">PHP array functions<\/a> that are frequently used while working with arrays.<\/p>\n<pre class=\"prettyprint\"><code class=\"language-php\">&lt;?php\n\/\/ Accented characters\n\/\/ to preserve accented characters during PHP Array to JSON conversion\n\/\/ you need to utf8 encode the values and then do json_encode\necho '&lt;h1&gt;For accented characters PHP Array to JSON&lt;\/h1&gt;';\n$array = array( 'w1' =&gt; 'r\u00e9sum\u00e9', 'w2' =&gt; 'ch\u00e2teau', 'w3' =&gt; 'fa\u00e7ade', 'w4' =&gt; 'd\u00e9j\u00e0 vu', 'w5' =&gt; 'S\u00e3o Paulo'\n);\n$utfEncodedArray = array_map(\"utf8_encode\", $array);\necho json_encode($utfEncodedArray);\n?&gt;\n<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"prettyprint\"><code>{\"w1\":\"r\\u00c3\\u00a9sum\\u00c3\\u00a9\",\"w2\":\"ch\\u00c3\\u00a2teau\",\"w3\":\"fa\\u00c3\\u00a7ade\",\"w4\":\"d\\u00c3\\u00a9j\\u00c3\\u00a0 vu\",\"w5\":\"S\\u00c3\\u00a3o Paulo\"}\n<\/code><\/pre>\n<h2 class=\"p1\">4) PHP Array to JSON with pretty-printing<\/h2>\n<p>It applies to prettyprint on the converted output JSON properties in a neet spacious format.<\/p>\n<p>The <a href=\"https:\/\/phppot.com\/php\/php-json-encode-and-decode\/\">PHP json_encode() function<\/a> accepts the second parameter to set the bitmask flag. This flag is used to set the JSON_PRETTY_PRINT to align the output JSON properties.<\/p>\n<pre class=\"prettyprint\"><code class=\"language-php\">&lt;?php\n\/\/ to neatly align the output with spaces\n\/\/ it may be useful when you plan to print the\n\/\/ JSON output in a raw format\n\/\/ helpful when debugging complex multidimensional PHP Arrays and JSON objects\n\/\/ lot more constants are available like this, which might be handy in situations\necho '&lt;h1&gt;Convert PHP Array to JSON and Pretty Print&lt;\/h1&gt;';\n$array = array( 'e1' =&gt; 1000, 'e2' =&gt; 1500, 'e3' =&gt; 2000, 'e4' =&gt; 2350, 'e5' =&gt; 3000\n);\necho json_encode($array, JSON_PRETTY_PRINT);\n?&gt;\n<\/code><\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"prettyprint\"><code>{ \"e1\": 1000, \"e2\": 1500, \"e3\": 2000, \"e4\": 2350, \"e5\": 3000 }\n<\/code><\/pre>\n<p><a class=\"download\" href=\"https:\/\/phppot.com\/downloads\/php\/php-array-to-json.zip\">Download<\/a><\/p>\n<p> <!-- #comments --> <\/p>\n<div class=\"related-articles\">\n<h2>Popular Articles<\/h2>\n<\/p><\/div>\n<p> <a href=\"https:\/\/phppot.com\/php\/php-array-to-json\/#top\" class=\"top\">\u2191 Back to Top<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>by Vincy. Last modified on October 22nd, 2022. JSON is the best format to transfer data over network. It is an easily parsable format comparatively. That\u2019s why most of the API accepts parameters and returns responses in JSON. There are online tools to convert an array to a JSON object. This tutorial teaches how to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":129145,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[65],"tags":[],"class_list":["post-129144","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-updates"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/129144","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=129144"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/129144\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/129145"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=129144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=129144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=129144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}