douduan3203
2018-06-08 07:44 阅读 114

使用PHP循环嵌套的JSON数组

I have a JSON array as follows:

[
{
    "custClass": [
        {
            "code": "50824109d3b1947c9d9390ac5caae0ef",
            "desc": "e1f96b98047adbc39f8baf8f4aa36f41"
        },
        {
            "code": "dab6cc0ed3688f96333d91fd979c5f74",
            "desc": "d0e850f728b2febee79e1e7d1186c126"
        },
        {
            "code": "bc4050f8f891296528ad6a292b615e86",
            "desc": "bee3120e77092d889c3b9e27cbee75bd"
        },
        {
            "code": "f13fc8c35dfe206a641207c6054dd9a0",
            "desc": "32a81cb610805d9255d5f11354177414"
        },
        {
            "code": "2117c346d9b3dfebf18acc8b022326d4",
            "desc": "88a8e85db11976082fed831c4c83838e"
        },
        {
            "code": "95c0674fc0e0434f52a60afce74571d2",
            "desc": "39c4d4bca1578194801f44339998e382"
        },
        {
            "code": "c8ad6f709612d2a91bb9f14c16798338",
            "desc": "6b4c4d5f4ae609742c1b6e62e16f8650"
        }
    ],
    "sourceData": [
        {
            "sourceId": "ff64060a40fc629abf24eb03a863fd55",
            "sourceName": "92aa69979215a2bf6290c9a312c5891f"
        }
    ]
}

]

I want to loop through this nested JSON array to retrieve all the "desc" from the "custClass" list using PHP.

Any help would be appreciated.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    doulou1970 doulou1970 2018-06-08 07:52

    You can decode data and loop it

         $s = '[
    {
        "custClass": [
            {
                "code": "50824109d3b1947c9d9390ac5caae0ef",
                "desc": "e1f96b98047adbc39f8baf8f4aa36f41"
            },
            {
                "code": "dab6cc0ed3688f96333d91fd979c5f74",
                "desc": "d0e850f728b2febee79e1e7d1186c126"
            },
            {
                "code": "bc4050f8f891296528ad6a292b615e86",
                "desc": "bee3120e77092d889c3b9e27cbee75bd"
            },
            {
                "code": "f13fc8c35dfe206a641207c6054dd9a0",
                "desc": "32a81cb610805d9255d5f11354177414"
            },
            {
                "code": "2117c346d9b3dfebf18acc8b022326d4",
                "desc": "88a8e85db11976082fed831c4c83838e"
            },
            {
                "code": "95c0674fc0e0434f52a60afce74571d2",
                "desc": "39c4d4bca1578194801f44339998e382"
            },
            {
                "code": "c8ad6f709612d2a91bb9f14c16798338",
                "desc": "6b4c4d5f4ae609742c1b6e62e16f8650"
            }
        ],
        "sourceData": [
            {
                "sourceId": "ff64060a40fc629abf24eb03a863fd55",
                "sourceName": "92aa69979215a2bf6290c9a312c5891f"
            }
        ]
    }
    ]';
    $data =json_decode($s,true);
    foreach($data as  $obj){
        foreach($obj['custClass'] as $val){ 
            echo "Desc ".$val['desc']."<br/>";
        }
    }
    
    点赞 评论 复制链接分享
  • dongtuo8170 dongtuo8170 2018-06-08 07:50

    You can try this way

    $json='{
      "custClass": [
        {
          "code": "50824109d3b1947c9d9390ac5caae0ef",
          "desc": "e1f96b98047adbc39f8baf8f4aa36f41"
        },
        {
          "code": "dab6cc0ed3688f96333d91fd979c5f74",
          "desc": "d0e850f728b2febee79e1e7d1186c126"
        },
        {
          "code": "bc4050f8f891296528ad6a292b615e86",
          "desc": "bee3120e77092d889c3b9e27cbee75bd"
        },
        {
          "code": "f13fc8c35dfe206a641207c6054dd9a0",
          "desc": "32a81cb610805d9255d5f11354177414"
        },
        {
          "code": "2117c346d9b3dfebf18acc8b022326d4",
          "desc": "88a8e85db11976082fed831c4c83838e"
        },
        {
          "code": "95c0674fc0e0434f52a60afce74571d2",
          "desc": "39c4d4bca1578194801f44339998e382"
        },
        {
          "code": "c8ad6f709612d2a91bb9f14c16798338",
          "desc": "6b4c4d5f4ae609742c1b6e62e16f8650"
        }
      ],
      "sourceData": [
        {
          "sourceId": "ff64060a40fc629abf24eb03a863fd55",
          "sourceName": "92aa69979215a2bf6290c9a312c5891f"
        }
      ]
    }';
    
            $decode=json_decode($json,true);
            $desc=[];
    
            foreach($decode['custClass'] as $cust){
                  $desc[]=$cust['desc'];
            }        
            var_dump($desc);
    
    点赞 评论 复制链接分享
  • dsuxcxqep31023992 dsuxcxqep31023992 2018-06-08 07:54

    Try decoding data and retrieve it using foreach:

    $your_data = your_data;
    
    $decoded_data = json_decode($your_data [0], true);
    $final_data = [];
    foreach($decoded_data['custClass'] as $data) {
    $final_data[] = $data['desc'];
    }
    
    print_r($final_data);
    
    点赞 评论 复制链接分享
  • dongmou2389 dongmou2389 2018-06-08 07:57

    try this code

    loop this array like below

    foreach(json_decode($data) as $key=>$value){
            foreach($value->custClass as $key1=>$value1){
                echo $value1->desc;
            }
        }
    

    json_decode() the data

    <?php
    $data= '[
    {
    "custClass": [
    {
    "code": "50824109d3b1947c9d9390ac5caae0ef",
    "desc": "e1f96b98047adbc39f8baf8f4aa36f41"
    },
    {
    "code": "dab6cc0ed3688f96333d91fd979c5f74",
    "desc": "d0e850f728b2febee79e1e7d1186c126"
    },
    {
    "code": "bc4050f8f891296528ad6a292b615e86",
    "desc": "bee3120e77092d889c3b9e27cbee75bd"
    },
    {
    "code": "f13fc8c35dfe206a641207c6054dd9a0",
    "desc": "32a81cb610805d9255d5f11354177414"
    },
    {
    "code": "2117c346d9b3dfebf18acc8b022326d4",
    "desc": "88a8e85db11976082fed831c4c83838e"
    },
    {
    "code": "95c0674fc0e0434f52a60afce74571d2",
    "desc": "39c4d4bca1578194801f44339998e382"
    },
    {
    "code": "c8ad6f709612d2a91bb9f14c16798338",
    "desc": "6b4c4d5f4ae609742c1b6e62e16f8650"
    }
    ],
    "sourceData": [
    {
    "sourceId": "ff64060a40fc629abf24eb03a863fd55",
    "sourceName": "92aa69979215a2bf6290c9a312c5891f"
    }
    ]
    }
    ]';
    foreach(json_decode($data) as $key=>$value){
        foreach($value->custClass as $key1=>$value1){
            echo $value1->desc;
        }
    }
    ?>
    
    点赞 评论 复制链接分享

相关推荐