如何在CI中解析嵌套的json数组

时间:2014-02-03 12:27:13

标签: foreach json var-dump

我正在尝试解析以下json

{
"offerinfo": {
    "offername": "chaos",
    "offertype": "3",
    "brand": "1",
    "subbrand": "0",
    "categories": "1,2",
    "Tags": "4,6",
    "promotiontype": "1",
    "currency": "1",
    "promotioncode": "1",
    "offertitle": "1",
    "offerdescription": "ad",
    "addtionalterms": "asdasd",
    "retaildescription": "asdasd"
},
"media": {
    "video": "",
    "images": []
},
"availability": [
    {
        "airports": "1,4,6",
        "stores": "3,4,5",
        "startdate": "2014",
        "enddate": "3434"
    }],
"featured": {
    "categories": "",
    "slot": ""
}

}

用于解析的COde是

    $json = json_decode(trim(file_get_contents('php://input')),true);
        //print_r($json)."<br/>";
        //var_dump($json);

        foreach($json as $key=>$value)
        {
            var_dump($value);
            $offername = $value['offername']."<br/>";
            //echo $media = $value['video'];
            $mediaimage = $value['images'];
            foreach($mediaimage as $a)
            {
                //echo $a['MediaFile'];
            }
            //echo $avail = $value['airports'];
            echo $key->{'availability'};
            //var_dump($avail);
        //  foreach($avail as $b)
        //  {
            //  echo $b['airports'];
            //} 


            //var_dump($mediaimage);
        }

我在ogger信息词典中获得了offername等,但我无法解析可用性信息

有人可以帮助我

1 个答案:

答案 0 :(得分:0)

$json['availability']是一个数组,因此您可以像这样访问它

foreach ($json['availability'] as $availability) {
    echo $availability["airports"];
    // etc
}