Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] PHP Array to JSON String Convert with Online Demo

#1
PHP Array to JSON String Convert with Online Demo

<div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2022/10/php-array-to-json-string-convert-with-online-demo.jpg" width="550" height="296" title="" alt="" /></div><div><div class="modified-on" readability="7.1304347826087"> by <a href="https://phppot.com/about/">Vincy</a>. Last modified on October 22nd, 2022.</div>
<p>JSON is the best format to transfer data over network. It is an easily parsable format comparatively. That’s why most of the API accepts parameters and returns responses in JSON.</p>
<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>
<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>
<h2>Quick example</h2>
<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>
<div class="post-section-highlight" readability="35">
<pre class="prettyprint"><code class="language-php">&lt;?php
$array = array(100, 250, 375, 400);
$jsonString = json_encode($array);
echo $jsonString;
?&gt;
</code></pre>
</div>
<p><a class="demo" href="https://phppot.com/demo/php-array-to-json/">View Demo</a></p>
<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>
<ol>
<li>Simple to complex PHP array to JSON.</li>
<li>Remove array keys before converting to JSON.</li>
<li>Convert PHP array with accented characters to JSON</li>
<li>PHP Array to JSON with pretty-printing</li>
</ol>
<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>
<p>See this online demo to convert an array of comma-separated values into a JSON object.</p>
<p><img 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/20...00x162.jpg 300w, https://phppot.com/wp-content/uploads/20...68x414.jpg 768w, https://phppot.com/wp-content/uploads/20...o-json.jpg 960w" sizes="(max-width: 550px) 100vw, 550px"></p>
<h2 class="p1">1) Simple to complex PHP array to JSON</h2>
<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>
<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>
<pre class="prettyprint"><code class="language-php">&lt;?php
// PHP Array to JSON string conversion for
// simple, associative and multidimensional arrays
// all works the same way using json_encode
// just present different arrays for example purposes only // simple PHP Array to JSON string
echo '&lt;h1&gt;PHP Array to JSON&lt;/h1&gt;';
$array = array( 100, 250, 375, 400
);
$jsonString = json_encode($array);
echo $jsonString; // Associative Array to JSON
echo '&lt;h2&gt;Associative PHP Array to JSON&lt;/h2&gt;';
$array = array( 'e1' =&gt; 1000, 'e2' =&gt; 1500, 'e3' =&gt; 2000, 'e4' =&gt; 2350, 'e5' =&gt; 3000
);
$jsonString = json_encode($array);
echo $jsonString; // multidimensional PHP Array to JSON string
echo '&lt;h2&gt;Multidimensional PHP Array to JSON&lt;/h2&gt;';
$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' )
);
echo json_encode($multiArray);
?&gt;
</code></pre>
<p><strong>Output:</strong></p>
<pre class="prettyprint"><code>//PHP Array to JSON
[100,250,375,400] //Associative PHP Array to JSON
{"e1":1000,"e2":1500,"e3":2000,"e4":2350,"e5":3000} //Multidimensional PHP Array to JSON
{"a1":{"item_id":1,"name":"Lion","type":"Wild","location":"Zoo"},"a2":{"item_id":2,"name":"Cat","type":"Domestic","location":"Home"}}
</code></pre>
<h2 class="p1">2) Remove array keys before converting to JSON</h2>
<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>
<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>
<ol>
<li>It applies PHP array_values() to read the value array.</li>
<li>Then, it applies json_encode on the values array.</li>
</ol>
<pre class="prettyprint"><code class="language-php">&lt;?php
// array_values() to remove assigned keys and convert to the original PHP Array key
echo '&lt;h1&gt;To remove assigned associative keys and PHP Array to JSON&lt;/h1&gt;';
$array = array( 'e1' =&gt; 1000, 'e2' =&gt; 1500, 'e3' =&gt; 2000, 'e4' =&gt; 2350, 'e5' =&gt; 3000
); $jsonString = json_encode(array_values($array));
echo $jsonString;
?&gt;
</code></pre>
<p><strong>Output:</strong></p>
<pre class="prettyprint"><code>[1000,1500,2000,2350,3000]
</code></pre>
<h2 class="p1">3) Convert the PHP array with accented characters to JSON</h2>
<p>It is also a two-step process to convert the array of data containing accented characters.</p>
<p>It applies UTF8 encoding on the array values before converting them into a JSON object.</p>
<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>
<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>
<pre class="prettyprint"><code class="language-php">&lt;?php
// Accented characters
// to preserve accented characters during PHP Array to JSON conversion
// you need to utf8 encode the values and then do json_encode
echo '&lt;h1&gt;For accented characters PHP Array to JSON&lt;/h1&gt;';
$array = array( 'w1' =&gt; 'résumé', 'w2' =&gt; 'château', 'w3' =&gt; 'façade', 'w4' =&gt; 'déjà vu', 'w5' =&gt; 'São Paulo'
);
$utfEncodedArray = array_map("utf8_encode", $array);
echo json_encode($utfEncodedArray);
?&gt;
</code></pre>
<p><strong>Output:</strong></p>
<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"}
</code></pre>
<h2 class="p1">4) PHP Array to JSON with pretty-printing</h2>
<p>It applies to prettyprint on the converted output JSON properties in a neet spacious format.</p>
<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>
<pre class="prettyprint"><code class="language-php">&lt;?php
// to neatly align the output with spaces
// it may be useful when you plan to print the
// JSON output in a raw format
// helpful when debugging complex multidimensional PHP Arrays and JSON objects
// lot more constants are available like this, which might be handy in situations
echo '&lt;h1&gt;Convert PHP Array to JSON and Pretty Print&lt;/h1&gt;';
$array = array( 'e1' =&gt; 1000, 'e2' =&gt; 1500, 'e3' =&gt; 2000, 'e4' =&gt; 2350, 'e5' =&gt; 3000
);
echo json_encode($array, JSON_PRETTY_PRINT);
?&gt;
</code></pre>
<p><strong>Output:</strong></p>
<pre class="prettyprint"><code>{ "e1": 1000, "e2": 1500, "e3": 2000, "e4": 2350, "e5": 3000 }
</code></pre>
<p><a class="download" href="https://phppot.com/downloads/php/php-array-to-json.zip">Download</a></p>
<p> <!-- #comments --> </p>
<div class="related-articles">
<h2>Popular Articles</h2>
</p></div>
<p> <a href="https://phppot.com/php/php-array-to-json/#top" class="top">↑ Back to Top</a> </p>
</div>


https://www.sickgaming.net/blog/2022/10/...line-demo/
Reply



Forum Jump:


Users browsing this thread:
3 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016