Speed up development with full-stack environments for every branch.

Learn More

JSON module example - How to encode an array in JSON format

Forked from Hello World.

209 Runs 602 Views 67 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<?php

/**
 * JSON
 * encode an array in JSON format using json_encode() function
 */
$arrayToBeEncoded = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'names' => array('john' => 'doe', 'michael' => 'jackson'), 'cars' => array('ferrari', 'lamborghini', 'lotus', 'bmw'));
//encodes the array in JSON format
$JSONformat = json_encode($arrayToBeEncoded);
echo '<center>';
echo '<br />
    <div style="background-color:yellow;color:#000;padding:10px;width:500px;font-size:16px">
    <b><u>Array to be encoded:</u></b><br />';
echo'<pre>';
print_r($arrayToBeEncoded);
echo '</pre>';
echo'</div>
    <br />';

echo '<br /><div style="background-color:green;color:#fff;padding:10px;width:700px;font-size:16px"><b><u>Array encoded in JSON format:</u></b><br /><br />' . $JSONformat . '</div><br />';
echo '</center>';
?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).