Hello

There is a JSON that looks like this:

{ "response": { "count": 10, "items": [{...}, {...}, {...}, {...}] } } 

Each element of the items array either contains or does not contain the copy_history array . In general, the items array looks like this:

 "items": [{ "id": 111, "from_id": -119473366, "owner_id": -119473366, "date": 1483779506, "marked_as_ads": 0, "post_type": "post", "text": "", "copy_history": [{ "id": 30679, "owner_id": -96126130, "from_id": -96126130, "date": 1483772560, "post_type": "post", "text": "", "attachments": [{ "type": "photo", "photo": { "id": 456240780, "album_id": -7, "owner_id": -96126130, "user_id": 100, "photo_75": "https://pp.vk.me/...a3e/0zDlnwXV97M.jpg", "photo_130": "https://pp.vk.me/...a3f/is7lDvyh3JE.jpg", "photo_604": "https://pp.vk.me/...a40/Z6vXcOY-cf4.jpg", "photo_807": "https://pp.vk.me/...a41/g9QedfvZoZg.jpg", "width": 640, "height": 521, "text": "", "date": 1483140936, "post_id": 29964, "access_key": "" } }] 

You need to go through each of the items, get the value of the id field from it, and find out if there is an copy_history array.

help me please. thanks in advance

    1 answer 1

    Logically, I would do this:

     $json = '{"response": {"count": 10, "items": [{...}, {...}, {...}, {...}] }}'; $arr = json_decode($json, true); $res = []; for($i = 0; $i < count($arr['response']['items']); $i++){ $copy_history = 0; if(isset($arr['response']['items'][$i]['copy_history'])){ $copy_history = 1; }; array_push($res, Array(['id'] => $arr['response']['items'][$i]['id'], ['copy_history'] => $copy_history)); }; 
    • It seems to be something, but I get the Illegal offset type in the array_push method - lounah
    • @moon_cherry, in id or copy_history ? - Yuri
    • array_push placed in if, which is higher - lounah
    • Thank you once again - lounah