<?Php
$my_data = new stdClass();
$my_data->name="plus2net";
$my_data->area="PHP";
$my_data->section="Json";
$out_data=json_encode($my_data);
echo $out_data;
?>
Output is here
{"name":"plus2net","area":"PHP","section":"Json"}
$a=array("Apple"=>10,"Orange"=>40,"Mango"=>80,"Banana"=>30);
echo json_encode($a);
The output of the above code is here.
{"Apple":10,"Orange":40,"Mango":80,"Banana":30}
This is one of the simple requirements, so we will try some complex than this by using two arrays.
<?Php
$my_data = new stdClass();
$my_data->name="plus2net";
$my_data->area="PHP";
$a=array("Apple"=>10,"Orange"=>40,"Mango"=>80);
$out_data=array($a,"data"=>$my_data);
echo json_encode($out_data);
?>
The output of the above code is here.
{"0":{"Apple":10,"Orange":40,"Mango":80},"data":{"name":"plus2net","area":"PHP"}}
You can start from here making your json string to exchange data across pages.
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.