Encode an associative array into a JSON object

                
                    <?php
                    $age = array("Peter"=>35, "Ben"=>37, "Joe"=>43);
                    
                    echo json_encode($age);
                    ?>
                
                

Decode JSON data into a PHP object

                
                    <?php
                    $jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';
                     
                    var_dump(json_decode($jsonobj));
                    ?>
                
                

Access the values from a PHP object

                
                    <?php
                    $jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';
                    
                    $obj = json_decode($jsonobj);
                    
                    echo $obj->Peter;
                    echo $obj->Ben;
                    echo $obj->Joe;
                    ?>
                
                

Loop through the values of a PHP object

                
                    <?php
                    $jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';
                    
                    $obj = json_decode($jsonobj);
                    
                    foreach($obj as $key => $value) {
                      echo $key . " => " . $value . "<br>";
                    }
                    ?>