ATIKON
ATIKON

Reputation: 447

Iterate json data php

firstly, sorry for my english. Now i parsing data from .json and I had a problem, by using foreach I can parse only 1 attribute data once. I need to retrieve src_big more than 1 times

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

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

For example:

{  
   "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
         }
      }
   ]
}

Upvotes: 2

Views: 130

Answers (3)

Chetan Ameta
Chetan Ameta

Reputation: 7896

if you are using PHP > 5.1.3 then you can use iterator. Have a look at below solution:

$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);

Output

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
)

Upvotes: 1

cFreed
cFreed

Reputation: 4484

Anyway you're using to explore your source JSON, an issue comes from its structure:

  • 1st existing src_big is a member of the attachment object
  • all other ones are members of objects in the attachments array

So you must either:

  • change the JSON structure (if you've hand on it) to make it consistent, i.e. the 1st attachment object is changed to an attachments array of objects
  • or iterate distinctly to look for each of the above structures

Upvotes: 1

Cihan Uygun
Cihan Uygun

Reputation: 2138

You can try json_decode as Try mentioned on comment here is an example of your question;

<?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);
    }
}

And working example is here http://ideone.com/8WRxcB

Hope this helps

Upvotes: 1

Related Questions