John Davidson

Get value from JSON and display it (foreach command) in PHP

0 comments
Message:


Hello I've been trying to solve this by myself for a few hours now, with zero luck. I'm trying to use the foreach command in php to display a decoded value from json.


{
-car_data: {
car_id: "87",
car_cost: "62000",

So let's say I want to display the value of car_id, then below that the value of car_cost.
I need to do this using the foreach command. Please briefly explain the process. I'd greatly appreciate it!



My Answer:
123, _car_make: "Toyota", _car_model: "Camry", _car_year: 2020_}_}

Here is the PHP code to decode the JSON and display the values using foreach:

php
$json = '{"car_data": {"car_id": 123, "car_make": "Toyota", "car_model": "Camry", "car_year": 2020}}';

$data = json_decode($json, true);

foreach ($data['car_data'] as $key => $value) {
echo $key . ': ' . $value . '
';
}


This code will output:


car_id: 123
car_make: Toyota
car_model: Camry
car_year: 2020

Rate this post

3 of 5 based on 2204 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us