Parsing JSON in PHP

前端 未结 2 1323
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-22 10:27

I´ve got the following JSON string:

{\"Data\":{\"Recipes\":{\"Recipe_5\":{\"ID\":\"5\",\"TITLE\":\"Spaghetti Bolognese\"},\"Recipe_7\":{\"ID\":\"7\",\"TITLE         


        
2条回答
  •  [愿得一人]
    2020-12-22 11:14

    You can use the function json_decode to parse JSON data in PHP (>= 5.2.0, at least). Once you have a PHP object, it should be easy to iterate over all recipes/members and access their titles, using something like this:

    $data = json_decode($json, true); // yields associative arrays instead of objects
    foreach ($data['Data']['Recipes'] as $key => $recipe) {
        echo $recipe['TITLE'];
    }
    

    (Sorry I can't actually run this code right now. Hope it helps anyway.)

提交回复
热议问题