How to Decode JSON Data in PHP?

Today, We want to share with you json decode php.In this post we will show you php json foreach key => $value, hear for php get json data from api we will give you demo and example for implement.In this post, we will learn about Create JSON with json_encode and json_decode using PHP with an example.

PHP json_decode() Function

here you can learn all about JSON Handling with PHP: How to Encode, Write, Parse, Decode and Convert.
JSON Functions

Function Libraries
json_encode Returns the JSON representation of a value.
json_decode Decodes a JSON string.
json_last_error Returns the last error occurred.
Read Also:  stringlength php - The Best PHP 3 Examples

Example 1:

<?php
$crickerterObject = '{"Virat":35,"Rohit":37,"Jasmrit":43}';

var_dump(json_decode($crickerterObject));
?>

Example 2: access the values from the PHP object:

<?php
$crickerterObject = '{"Virat":35,"Rohit":37,"Jasmrit":43}';

$obj = json_decode($crickerterObject);

echo $obj->Virat;
echo $obj->Rohit;
echo $obj->Jasmrit;
?>

Example 3: access the values from

<?php
$crickerterObject = '{"Virat":35,"Rohit":37,"Jasmrit":43}';

$data_array = json_decode($crickerterObject, true);

echo $data_array["Virat"];
echo $data_array["Rohit"];
echo $data_array["Jasmrit"];
?>

json_decode to array

$result = json_decode($jsondata, true);

//OR

$result = array_values(json_decode($jsondata, true));

//access it as an object:

print_r($obj->Result);

how to convert an array into JSON with PHP?

<?php
   $data_array = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
   echo json_encode($data_array);
?>

JSON Handling with PHP by Parsing File Data

Json File

{
	"Cricket_World_Cup_finals":
	[ 
		{
			"Year": "2021",
			"data": 
			{
				"Winner": "Sachin",
				"Score": "4-2",
				"Runner-up": "Khijadiya"
			}
		},
	    {
			"Year": "2019",
			"data": 
			{
				"Winner": "Kajal",
				"Score": "1-0",
				"Runner-up": "NagarPipaliya"
			}
		},
	    {
			"Year": "2015",
			"data": 
			{
				"Winner": "Sandeep",
				"Score": "1-0",
				"Runner-up": "Rajkot"
			}
		}
	]
}

use of the power of PHP built-in functions : RecursiveIteratorIterator

<?php 
$JSON = file_get_contents("input.json");
$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($JSON, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST);

foreach ($jsonIterator as $key => $val) {
    if(!is_array($val)) {
        if($key == "Year") {
            print "<br/>";
        }
    print $key."    : ".$val . "<br/>";
    }
}
?>

I hope you get an idea about json_decode multidimensional array php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.