Find the output of PHP programs | JSON | Set 1: Enhance the knowledge of PHP JSON by solving and finding the output of some PHP programs.
Question 1:
<?php
$student = array(
"Rahul" => 101,
"Rohit" => 102,
"Virat" => 103
);
$JSON_STR = encode_json($student);
echo $JSON_STR;
?>
Question 2:
<?php
$student = array(
"Rahul" => 101,
"Rohit" => 102,
"Virat" => 103
);
$JSON_STR = json_encode($student);
$STR = json_decode($JSON_STR);
var_dump($STR);
?>
Question 3:
<?php
$student = array(
"Rahul" => "ABC",
"Rohit" => "PQR",
"Virat" => "XYZ"
);
$JSON_STR = json_encode($student);
$STR = json_decode($JSON_STR);
echo $STR->ABC . '<br>';
echo $STR->PQR . '<br>';
echo $STR->XYZ . '<br>';
?>
Question 4:
<?php
$student = array(
"Rahul" => "ABC",
"Rohit" => "PQR",
"Virat" => "XYZ"
);
$JSON_STR = json_encode($student);
$STR = json_decode($JSON_STR);
echo $STR['Rahul'] . '<br>';
echo $STR['Rohit'] . '<br>';
echo $STR['Virat'] . '<br>';
?>
Question 5:
<?php
$student = array(
"Rahul" => "ABC",
"Rohit" => "PQR",
"Virat" => "XYZ"
);
$JSON_STR = json_encode($student);
$STR = json_decode($JSON_STR);
foreach ($STR as $key => $value)
echo $key . " => " . $value . "<br>";
?>
Answer 1:
Output:
Explanation:
The above program will generate compilation error, because encode_json() is not a built function in PHP. The json_encode() function is used to encode an array into JSON format.
Answer 2:
Output:
Explanation:
In the above program, we created an array $student that contain student information. Then we encode array into JSON format and assigned to $JSON_STR and then we decode $JSON_STR using json_decode() function and assigned to $STR. Then we call var_dump() function and pass $STR to it then the above output will print on the webpage.
Answer 3:
Output:
Explanation:
In the above program, we created an associative array that contains keys ("Rahul", "Rohit", "Virat") and values ("ABC","PQR","XYZ"). Then we encode string $student and then we decode json_decode() into $STR.
Here, we access values directly using operator '->', we can access array values using specified keys.
echo $STR->Rahul.'<br>'; echo $STR->Rohit.'<br>'; echo $STR->Virat.'<br>';
In the above, echo statements we accessed values of associative array.
Answer 4:
Output:
Explanation:
In the above program, we created an associative array that contains keys ("Rahul", "Rohit", "Virat") and values ("ABC","PQR","XYZ"). Then we encode string $student and then we decode json_decode() into $STR.
The above program will generate syntax error, because $STR is not an array. it is an object returned by json_decode() function.
The correct way is given below,
echo $STR->Rahul.'<br>'; echo $STR->Rohit.'<br>'; echo $STR->Virat.'<br>';
Answer 5:
Output:
Explanation:
In the above program, we created an associative array that contains keys ("Rahul", "Rohit", "Virat") and values ("ABC","PQR","XYZ"). Then we encode string $student and then we decode json_decode() into $STR.
Then we accessed keys and values using foreach loop and print them on the webpage.
need an explanation for this answer? contact us directly to get an explanation for this answer