How to analyze the following array (array >> object >> fwd_messages - right up to the last element):

 { "type": "message_new", "object": { "id": 40, "date": 1465562141, "out": 0, "user_id": 0, "read_state": 0, "title": " ... ", "body": "", "fwd_messages": [{ "user_id": 1, "date": 1465562135, "body": "", "fwd_messages": [{ "user_id": 1, "date": 1465562132, "body": "", "fwd_messages": [{ "user_id": 1, "date": 1465562129, "body": "", "fwd_messages": [{ "user_id": 1, "date": 1465562126, "body": "", "fwd_messages": [{ "user_id": 1, "date": 1465562123, "body": "1" }] }] }] }] }] }, "group_id": 0 } 

    1 answer 1

    Well, depending on what you need to get. For example, it is possible through a function with recursion. This example displays all date values ​​from fwd_messages.

     $json = '{"type":"message_new","object":{"id":40,"date":1465562141,"out":0,"user_id":0,"read_state":0,"title":" ... ","body":"","fwd_messages":[{"user_id":1,"date":1465562135,"body":"","fwd_messages":[{"user_id":1,"date":1465562132,"body":"","fwd_messages":[{"user_id":1,"date":1465562129,"body":"","fwd_messages":[{"user_id":1,"date":1465562126,"body":"","fwd_messages":[{"user_id":1,"date":1465562123,"body":"1"}]}]}]}]}]},"group_id":0}'; $arr = json_decode($json, true); function getFwd_messages($arr) { if (isset($arr['object']['fwd_messages'])) { getFwd_messages($arr['object']['fwd_messages'][0]); } elseif (isset($arr['user_id'])){ echo ('date >> ' . $arr['date']) . '<br>'; if(isset($arr['fwd_messages'])) { getFwd_messages($arr['fwd_messages'][0]); } } } getFwd_messages($arr); // date >> 1465562135 // date >> 1465562132 // date >> 1465562129 // date >> 1465562126 // date >> 1465562123 
    • why such difficulties when it is solved simply by passing into foreach() . - Naumov
    • I just want to, I also want to see a simpler and more correct solution, please send your option. - 5f0f5
    • @Naumov what difficulties? It's just a recursion. You will end up with the same thing, the maximum is a bit shorter - Vasily Barbashev