简体   繁体   中英

PHP json decode data using foreach

I have this following link and i want to print every item with id using foreach from json decoded string. Do you know how can i get these items using foreach? sample JSON data

Here is a code sample:

$json_data = file_get_contents("http://ddragon.leagueoflegends.com/cdn/6.24.1/data/en_US/profileicon.json");

$data = json_decode($json_data, true);

$items = $data['data'];

foreach($items as $item){
    // Here you can get $item['id'] and $item['image'] and echo or do what you want with that data
}

Why wouldn't you use function json_decode dedicated for this, that creates object with data? You can then iterate over this object.

Load data via cURL :

$data = json_decode($your_json_data, 1);
foreach($data as $item) 
{
    var_dump($item);
    //...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM