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

Learn More

JSON module example - How to decode a JSON string into an array [json_decode]

Forked from Hello World.

1292 Runs 7618 Views 1143 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<?php

/**
 * JSON
 * decode a JSON string into an array using json_decode() function
 */
$jsonToBeDecoded = '{"a":1,"b":2,"c":3,"d":4,"e":5,"names":{"john":"doe","michael":"jackson"},"cars":["ferrari","lamborghini","lotus","bmw"]}';
//decode the JSON into an array if hte second param is set to true otherwise the JSON will be decoded into an object
$arrayDecodedFromJSON = json_decode($jsonToBeDecoded, true);
echo '<center>';
echo '<br /><div style="background-color:green;color:#fff;padding:10px;width:700px;font-size:16px"><b><u>JSON to be decoded:</u></b><br /><br />' . $jsonToBeDecoded . '</div><br />';
echo '<br />
    <div style="background-color:yellow;color:#000;padding:10px;width:500px;font-size:16px">
    <b><u>Array resulted from JSON decode:</u></b><br />';
echo'<pre>';
print_r($arrayDecodedFromJSON);
echo '</pre>';
echo'</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).