How to iterate nested JSON in PHP

preview_player
Показать описание
In this video, I have shown you how to iterate through a nested JSON in PHP.
You can parse nested JSON in PHP decode function and then iterate it using foreach loop. If there are any array inside it again run foreach loop.
Рекомендации по теме
Комментарии
Автор

I'm really grateful, this video has fully answered my query.

dansonmwangi
Автор

Very good, this helped me. But instead, I made it a function and called it recursively:
<?php

$nested_json = '{
"name":"Ram",
"age": 27,
"vehicles": {
"car": "limousine",
"bike": "ktm-duke",
"plane": "lufthansa"
}
}';

$json_array = json_decode($nested_json, true);
walk_json($json_array);

echo "Hello world\n";


//=== Walk through the JSON

function walk_json($p_json) {

foreach ($p_json as $key => $value ) {
if (is_array($value)) {
walk_json($value);
}else{
echo $key, " = ", $value, "\n";
}//endif
}//endForEach

}//endFunction

?>

craigh