how to get data from json array in php? (php json parsing)

Today, We want to share with you php json parsing.In this post we will show you php json_decode example, hear for how to get data from json array in php? 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.

How to Encode and Decode JSON Data in PHP?

{
    "book": {
        "name": "Krunal Sisodiya and Ankit kathiriya",
        "author": "I. D. Parmar",
        "year": 2021,
        "genre": "Welcome Pakainfo",
        "bestseller": true
    }
}

{
    "languages": [
        "Tamil",
        "Hindi",
        "English",
        "Gujrati"
    ]
}

Encoding JSON Data in PHP

<?php
$marks = array("Dhara"=>65, "Ravee"=>80, "Kene"=>78, "Radha"=>90);
 
echo json_encode($marks);
?>

{"Dhara":65,"Ravee":80,"Kene":78,"Radha":90}

encode the PHP indexed array into a JSON array

<?php
$languages = array("Tamil", "Hindi", "English", "Gujrati", "Malayam");
 
echo json_encode($languages);
?>

["Tamil","Hindi","English","Gujrati","Malayam"]

using the JSON_FORCE_OBJECT option,

<?php

$languages = array("Tamil", "Hindi", "English", "Gujrati");
 
echo json_encode($languages, JSON_FORCE_OBJECT);
?>

{"0":"Tamil","1":"Hindi","2":"English","3":"Gujrati"}

Decoding JSON Data in PHP

<?php

$json = '{"Dhara":65,"Ravee":80,"Kene":78,"Radha":90}';
 
var_dump(json_decode($json));
?>

object(stdClass)#1 (4) { ["Dhara"]=> int(65) ["Ravee"]=> int(80) ["Kene"]=> int(78) ["Radha"]=> int(90) }

By default the json_decode() function

<?php

$json = '{"Dhara":65,"Ravee":80,"Kene":78,"Radha":90}';
 
var_dump(json_decode($json, true));
?>

array(4) { ["Dhara"]=> int(65) ["Ravee"]=> int(80) ["Kene"]=> int(78) ["Radha"]=> int(90) }

JSON object or array in PHP

<?php

$json = '{"Dhara":65,"Ravee":80,"Kene":78,"Radha":90}';
 

$arr = json_decode($json, true);

echo $arr["Dhara"];  // Result: 65
echo $arr["Ravee"];  // Result: 80
echo $arr["Kene"];   // Result: 78
echo $arr["Radha"];  // Result: 90
 

$obj = json_decode($json);

echo $obj->Dhara;   // Result: 65
echo $obj->Ravee;   // Result: 80
echo $obj->Kene;    // Result: 78
echo $obj->Radha;   // Result: 90
?>

decoded data using foreach() loop

<?php

$json = '{"Dhara":65,"Ravee":80,"Kene":78,"Radha":90}';
 

$arr = json_decode($json, true);
 

foreach($arr as $key=>$value){
    echo $key . "=>" . $value . "<br>";
}
echo "<hr>";

$obj = json_decode($json);
 

foreach($obj as $key=>$value){
    echo $key . "=>" . $value . "<br>";
}
?>

Extracting Values from Nested JSON Data in PHP

<?php
function printValues($arr) {
    global $count;
    global $values;
    

    if(!is_array($arr)){
        die("ERROR: Input is not an array");
    }
    

I hope you get an idea about php read json file.
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.