Read Json/Array string in Php

后端 未结 2 583
悲哀的现实
悲哀的现实 2020-12-12 02:06

I have a string which is in valid Json format and it looks like this:

{
    \"message\": \"success\",
    \"result\": {
        \"46620\": {
            \"co         


        
相关标签:
2条回答
  • 2020-12-12 02:21

    To loop through all the results, something like this should do it:

    $obj = json_decode($str);
    foreach ($obj->result as $result)
    {
        $fa_title = $result->fa_title;
        $en_title = $result->en_title;
    }
    
    0 讨论(0)
  • 2020-12-12 02:30

    In JSON, arrays are designated by brackets ([ and ]) while dictionaries only have braces ({ and }).

    You could access each result object individually by doing $obj['result']['46620']['en_title']. The result object is an associative array (also known as a dictionary) of associative arrays.

    You can also iterate through each object using a foreach loop, as demonstrated in this question

    0 讨论(0)
提交回复
热议问题