w3resource
php Programming Exercies

PHP JSON Exercises : Get JSON representation of a value from an array

PHP JSON: Exercise-3 with Solution

Write a PHP script to get JSON representation of a value from an array.

Sample Solution:

PHP Code:

<?php
$arra = array("uglify-js"=> "1.3.4", "jshint"=> "0.9.1", "recess"=> "1.1.8" ,"connect"=> "2.1.3", "hogan.js"=>"2.0.0"); 
$myarray = array('red', 'green', 'white');
var_dump(json_encode($arra));
echo "\n";
var_dump(json_encode($myarray));
?>

Sample Output:

string(92) "{"uglify-js":"1.3.4","jshint":"0.9.1","recess":"
1.1.8","connect":"2.1.3","hogan.js":"2.0.0"}"               
                                                            
string(23) "["red","green","white"]"

Flowchart :

Flowchart: Get JSON representation of a value from an array

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP script to decode large integers.
Next: Write a PHP function to display JSON decode errors.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming