迭代json数据php

时间:2016-03-02 20:07:18

标签: php arrays json

首先,对不起我的英语。 现在我从.json解析数据并且我遇到了问题,通过使用foreach我只能解析一次属性数据。 我需要检索src_big超过1次

"src_big":"http:\/\/cs7004.vk.me\/v7004692\/1af45\/QRt-0dPZ8qg.jpg"

"src_big":"http:\/\/cs7004.vk.me\/v7004767\/1ab37\/3M52LmF_dY8.jpg"

例如:

{  
   "response":[  
      {  
         "id":14867,
         "from_id":-65923388,
         "to_id":-65923388,
         "date":1456947602,
         "post_type":"post",
         "text":"Горячие клавиши Windows 10",
         "attachment":{  
            "type":"photo",
            "photo":{  
               "pid":404384423,
               "aid":223686536,
               "owner_id":-65923388,
               "user_id":100,
               "src":"http:\/\/cs7004.vk.me\/v7004692\/1af44\/5zhYes1I7xg.jpg",
               "src_big":"http:\/\/cs7004.vk.me\/v7004692\/1af45\/QRt-0dPZ8qg.jpg",
               "src_small":"http:\/\/cs7004.vk.me\/v7004692\/1af43\/msvfzwgm5tc.jpg",
               "width":604,
               "height":302,
               "text":"Наука<br>http:\/\/vk.com\/public65923388",
               "created":1456235510,
               "access_key":"d1cff88abc28e29b28"
            }
         },
         "attachments":[  
            {  
               "type":"photo",
               "photo":{  
                  "pid":404384423,
                  "aid":223686536,
                  "owner_id":-65923388,
                  "user_id":100,
                  "src":"http:\/\/cs7004.vk.me\/v7004692\/1af44\/5zhYes1I7xg.jpg",
                  "src_big":"http:\/\/cs7004.vk.me\/v7004692\/1af45\/QRt-0dPZ8qg.jpg",
                  "src_small":"http:\/\/cs7004.vk.me\/v7004692\/1af43\/msvfzwgm5tc.jpg",
                  "width":604,
                  "height":302,
                  "text":"Наука<br>http:\/\/vk.com\/public65923388",
                  "created":1456235510,
                  "access_key":"d1cff88abc28e29b28"
               }
            },
            {  
               "type":"photo",
               "photo":{  
                  "pid":404384428,
                  "aid":223686536,
                  "owner_id":-65923388,
                  "user_id":100,
                  "src":"http:\/\/cs7004.vk.me\/v7004365\/1a9ff\/3tEb3pm-bBA.jpg",
                  "src_big":"http:\/\/cs7004.vk.me\/v7004365\/1aa00\/LIQOncL-su8.jpg",
                  "src_small":"http:\/\/cs7004.vk.me\/v7004365\/1a9fe\/TKchasZfhNo.jpg",
                  "width":604,
                  "height":585,
                  "text":"Наука<br>http:\/\/vk.com\/public65923388",
                  "created":1456235512,
                  "access_key":"e9370296ef33bebb7a"
               }
            },
            {  
               "type":"photo",
               "photo":{  
                  "pid":404384439,
                  "aid":223686536,
                  "owner_id":-65923388,
                  "user_id":100,
                  "src":"http:\/\/cs7004.vk.me\/v7004767\/1ab36\/1IqODh-OmS0.jpg",
                  "src_big":"http:\/\/cs7004.vk.me\/v7004767\/1ab37\/3M52LmF_dY8.jpg",
                  "src_small":"http:\/\/cs7004.vk.me\/v7004767\/1ab35\/lOR1yvNeE8Y.jpg",
                  "width":478,
                  "height":604,
                  "text":"Наука<br>http:\/\/vk.com\/public65923388",
                  "created":1456235514,
                  "access_key":"8cc1048e72e29b8ced"
               }
            },
            {  
               "type":"photo",
               "photo":{  
                  "pid":404384504,
                  "aid":223686536,
                  "owner_id":-65923388,
                  "user_id":100,
                  "src":"http:\/\/cs7004.vk.me\/v7004474\/1b385\/wdi9GlRth20.jpg",
                  "src_big":"http:\/\/cs7004.vk.me\/v7004474\/1b386\/I5PY-gN_xa4.jpg",
                  "src_small":"http:\/\/cs7004.vk.me\/v7004474\/1b384\/HkzVl-hsZUo.jpg",
                  "width":604,
                  "height":419,
                  "text":"Наука<br>http:\/\/vk.com\/public65923388",
                  "created":1456235537,
                  "access_key":"5d5ca96b04e91e2744"
               }
            },
            {  
               "type":"photo",
               "photo":{  
                  "pid":404384509,
                  "aid":223686536,
                  "owner_id":-65923388,
                  "user_id":100,
                  "src":"http:\/\/cs7004.vk.me\/v7004452\/1a619\/nDL7Q9dJhxs.jpg",
                  "src_big":"http:\/\/cs7004.vk.me\/v7004452\/1a61a\/PaJXcLeD5Aw.jpg",
                  "src_small":"http:\/\/cs7004.vk.me\/v7004452\/1a618\/-h36pYEcpPY.jpg",
                  "width":604,
                  "height":292,
                  "text":"Наука<br>http:\/\/vk.com\/public65923388",
                  "created":1456235540,
                  "access_key":"ae318183e5303414e9"
               }
            }
         ],
         "comments":{  
            "count":0
         },
         "likes":{  
            "count":127
         },
         "reposts":{  
            "count":45
         }
      }
   ]
}

3 个答案:

答案 0 :(得分:1)

您可以尝试使用json_decode,因为此处的评论中提到的Try是您问题的一个示例;

<?PHP


$myJson = <<<JSON
{"response":[{"id":14867,"from_id":-65923388,"to_id":-65923388,"date":1456947602,"post_type":"post","text":"Горячие клавиши Windows 10","attachment":{"type":"photo","photo":{"pid":404384423,"aid":223686536,"owner_id":-65923388,"user_id":100,"src":"http:\/\/cs7004.vk.me\/v7004692\/1af44\/5zhYes1I7xg.jpg","src_big":"http:\/\/cs7004.vk.me\/v7004692\/1af45\/QRt-0dPZ8qg.jpg","src_small":"http:\/\/cs7004.vk.me\/v7004692\/1af43\/msvfzwgm5tc.jpg","width":604,"height":302,"text":"Наука<br>http:\/\/vk.com\/public65923388","created":1456235510,"access_key":"d1cff88abc28e29b28"}},"attachments":[{"type":"photo","photo":{"pid":404384423,"aid":223686536,"owner_id":-65923388,"user_id":100,"src":"http:\/\/cs7004.vk.me\/v7004692\/1af44\/5zhYes1I7xg.jpg","src_big":"http:\/\/cs7004.vk.me\/v7004692\/1af45\/QRt-0dPZ8qg.jpg","src_small":"http:\/\/cs7004.vk.me\/v7004692\/1af43\/msvfzwgm5tc.jpg","width":604,"height":302,"text":"Наука<br>http:\/\/vk.com\/public65923388","created":1456235510,"access_key":"d1cff88abc28e29b28"}},{"type":"photo","photo":{"pid":404384428,"aid":223686536,"owner_id":-65923388,"user_id":100,"src":"http:\/\/cs7004.vk.me\/v7004365\/1a9ff\/3tEb3pm-bBA.jpg","src_big":"http:\/\/cs7004.vk.me\/v7004365\/1aa00\/LIQOncL-su8.jpg","src_small":"http:\/\/cs7004.vk.me\/v7004365\/1a9fe\/TKchasZfhNo.jpg","width":604,"height":585,"text":"Наука<br>http:\/\/vk.com\/public65923388","created":1456235512,"access_key":"e9370296ef33bebb7a"}},{"type":"photo","photo":{"pid":404384439,"aid":223686536,"owner_id":-65923388,"user_id":100,"src":"http:\/\/cs7004.vk.me\/v7004767\/1ab36\/1IqODh-OmS0.jpg","src_big":"http:\/\/cs7004.vk.me\/v7004767\/1ab37\/3M52LmF_dY8.jpg","src_small":"http:\/\/cs7004.vk.me\/v7004767\/1ab35\/lOR1yvNeE8Y.jpg","width":478,"height":604,"text":"Наука<br>http:\/\/vk.com\/public65923388","created":1456235514,"access_key":"8cc1048e72e29b8ced"}},{"type":"photo","photo":{"pid":404384504,"aid":223686536,"owner_id":-65923388,"user_id":100,"src":"http:\/\/cs7004.vk.me\/v7004474\/1b385\/wdi9GlRth20.jpg","src_big":"http:\/\/cs7004.vk.me\/v7004474\/1b386\/I5PY-gN_xa4.jpg","src_small":"http:\/\/cs7004.vk.me\/v7004474\/1b384\/HkzVl-hsZUo.jpg","width":604,"height":419,"text":"Наука<br>http:\/\/vk.com\/public65923388","created":1456235537,"access_key":"5d5ca96b04e91e2744"}},{"type":"photo","photo":{"pid":404384509,"aid":223686536,"owner_id":-65923388,"user_id":100,"src":"http:\/\/cs7004.vk.me\/v7004452\/1a619\/nDL7Q9dJhxs.jpg","src_big":"http:\/\/cs7004.vk.me\/v7004452\/1a61a\/PaJXcLeD5Aw.jpg","src_small":"http:\/\/cs7004.vk.me\/v7004452\/1a618\/-h36pYEcpPY.jpg","width":604,"height":292,"text":"Наука<br>http:\/\/vk.com\/public65923388","created":1456235540,"access_key":"ae318183e5303414e9"}}],"comments":{"count":0},"likes":{"count":127},"reposts":{"count":45}}]}
JSON;

$myObject = json_decode($myJson);
foreach($myObject->response AS $index=>$item)
{
    //print_r($item); //Remove comment to see your item
    foreach($item->attachments AS $attachment)
    {
        echo "attachment:";
        print_r($attachment);
    }
}

工作示例在这里http://ideone.com/8WRxcB

希望这有帮助

答案 1 :(得分:1)

无论如何,你正在使用它来探索你的源JSON,问题来自它的结构:

  • 第一个现有src_big attachment对象的成员
  • 所有其他人都是attachments数组
  • 对象的成员

所以你必须:

  • 更改JSON结构(如果您已经提交)以使其保持一致,即第一个attachment对象更改为attachments对象数组
  • 或明确迭代以查找上述每个结构

答案 2 :(得分:1)

如果您使用PHP > 5.1.3,则可以使用迭代器。看看下面的解决方案:

$json = '{
   "response":[
      {
         "id":14867,
         "from_id":-65923388,
         "to_id":-65923388,
         "date":1456947602,
         "post_type":"post",
         "text":"Горячие клавиши Windows 10",
         "attachment":{
            "type":"photo",
            "photo":{
               "pid":404384423,
               "aid":223686536,
               "owner_id":-65923388,
               "user_id":100,
               "src":"http:\/\/cs7004.vk.me\/v7004692\/1af44\/5zhYes1I7xg.jpg",
               "src_big":"http:\/\/cs7004.vk.me\/v7004692\/1af45\/QRt-0dPZ8qg.jpg",
               "src_small":"http:\/\/cs7004.vk.me\/v7004692\/1af43\/msvfzwgm5tc.jpg",
               "width":604,
               "height":302,
               "text":"Наука<br>http:\/\/vk.com\/public65923388",
               "created":1456235510,
               "access_key":"d1cff88abc28e29b28"
            }
         },
         "attachments":[
            {
               "type":"photo",
               "photo":{
                  "pid":404384423,
                  "aid":223686536,
                  "owner_id":-65923388,
                  "user_id":100,
                  "src":"http:\/\/cs7004.vk.me\/v7004692\/1af44\/5zhYes1I7xg.jpg",
                  "src_big":"http:\/\/cs7004.vk.me\/v7004692\/1af45\/QRt-0dPZ8qg.jpg",
                  "src_small":"http:\/\/cs7004.vk.me\/v7004692\/1af43\/msvfzwgm5tc.jpg",
                  "width":604,
                  "height":302,
                  "text":"Наука<br>http:\/\/vk.com\/public65923388",
                  "created":1456235510,
                  "access_key":"d1cff88abc28e29b28"
               }
            },
            {
               "type":"photo",
               "photo":{
                  "pid":404384428,
                  "aid":223686536,
                  "owner_id":-65923388,
                  "user_id":100,
                  "src":"http:\/\/cs7004.vk.me\/v7004365\/1a9ff\/3tEb3pm-bBA.jpg",
                  "src_big":"http:\/\/cs7004.vk.me\/v7004365\/1aa00\/LIQOncL-su8.jpg",
                  "src_small":"http:\/\/cs7004.vk.me\/v7004365\/1a9fe\/TKchasZfhNo.jpg",
                  "width":604,
                  "height":585,
                  "text":"Наука<br>http:\/\/vk.com\/public65923388",
                  "created":1456235512,
                  "access_key":"e9370296ef33bebb7a"
               }
            },
            {
               "type":"photo",
               "photo":{
                  "pid":404384439,
                  "aid":223686536,
                  "owner_id":-65923388,
                  "user_id":100,
                  "src":"http:\/\/cs7004.vk.me\/v7004767\/1ab36\/1IqODh-OmS0.jpg",
                  "src_big":"http:\/\/cs7004.vk.me\/v7004767\/1ab37\/3M52LmF_dY8.jpg",
                  "src_small":"http:\/\/cs7004.vk.me\/v7004767\/1ab35\/lOR1yvNeE8Y.jpg",
                  "width":478,
                  "height":604,
                  "text":"Наука<br>http:\/\/vk.com\/public65923388",
                  "created":1456235514,
                  "access_key":"8cc1048e72e29b8ced"
               }
            },
            {
               "type":"photo",
               "photo":{
                  "pid":404384504,
                  "aid":223686536,
                  "owner_id":-65923388,
                  "user_id":100,
                  "src":"http:\/\/cs7004.vk.me\/v7004474\/1b385\/wdi9GlRth20.jpg",
                  "src_big":"http:\/\/cs7004.vk.me\/v7004474\/1b386\/I5PY-gN_xa4.jpg",
                  "src_small":"http:\/\/cs7004.vk.me\/v7004474\/1b384\/HkzVl-hsZUo.jpg",
                  "width":604,
                  "height":419,
                  "text":"Наука<br>http:\/\/vk.com\/public65923388",
                  "created":1456235537,
                  "access_key":"5d5ca96b04e91e2744"
               }
            },
            {
               "type":"photo",
               "photo":{
                  "pid":404384509,
                  "aid":223686536,
                  "owner_id":-65923388,
                  "user_id":100,
                  "src":"http:\/\/cs7004.vk.me\/v7004452\/1a619\/nDL7Q9dJhxs.jpg",
                  "src_big":"http:\/\/cs7004.vk.me\/v7004452\/1a61a\/PaJXcLeD5Aw.jpg",
                  "src_small":"http:\/\/cs7004.vk.me\/v7004452\/1a618\/-h36pYEcpPY.jpg",
                  "width":604,
                  "height":292,
                  "text":"Наука<br>http:\/\/vk.com\/public65923388",
                  "created":1456235540,
                  "access_key":"ae318183e5303414e9"
               }
            }
         ],
         "comments":{
            "count":0
         },
         "likes":{
            "count":127
         },
         "reposts":{
            "count":45
         }
      }
   ]
}';
$json_array = json_decode($json, true);
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($json_array));
$src_big = array();
foreach ($iterator as $key => $value) {
        //check key and dulicate entry
        if($key =='src_big' && in_array($value, $src_big) === false){
            $src_big[] = $value;
        }
}

print_r($src_big);

<强>输出

Array
(
    [0] => http://cs7004.vk.me/v7004692/1af45/QRt-0dPZ8qg.jpg
    [1] => http://cs7004.vk.me/v7004365/1aa00/LIQOncL-su8.jpg
    [2] => http://cs7004.vk.me/v7004767/1ab37/3M52LmF_dY8.jpg
    [3] => http://cs7004.vk.me/v7004474/1b386/I5PY-gN_xa4.jpg
    [4] => http://cs7004.vk.me/v7004452/1a61a/PaJXcLeD5Aw.jpg
)