How To Write Object in Json File


<?php


$student = new Stdclass();

$student->name = "ravi";

$student->mobile = "9450001112";

$student->email = "ravi@gmail.com";


//Encode it into Json

header("Content-Type:application/json");

$json=json_encode($student,JSON_PRETTY_PRINT);


//File Handling

$fp=fopen("output.json","w"); #Writing the File Json

if($fp==NULL){

die("Cannot Process the File");

}else

{

//write the data

fwrite($fp,$json);

fclose($fp);

}



Used to write Complete Data into a File



<?php


$student = new Stdclass();


$student->name = "Awnish";

$student->mobile = "9450001112";

$student->email = "Awnish@gmail.com";


//Encode it into Json

header("Content-Type:application/json");

$json=json_encode($student,JSON_PRETTY_PRINT);


//Used to write Complete Data into a File

file_put_contents("output.json",$json);



How To Write Object in Json File