How to encode and decode JSON in PHP?

decode

<?php

$student_data= '{"Ram":96,"Prashant":76,"Varun":65,"Mark":34}';// Decoding above JSON String into JSON object$decoded= json_decode($student_data);// Dump the $decoded variablevar_dump($decoded);?>

encode

<?php// PHP associative array$student_data= array("Ram"=>96, "Prashant"=>76, "Varun"=>65, "Mark"=>34);// Encoding PHP Associative array using json_encode()$encoded= json_encode($student_data);// Echo the dataecho$encoded;?>

Output:

{"Ram":96,"Prashant":76,"Varun":65,"Mark":34} 

Leave a comment

Your email address will not be published. Required fields are marked *