как получить данные из JSON?

у меня есть данные в JSON что-то вроде этого:

stdClass Object
(
  [contacts] => stdClass Object
    (
        [14] => stdClass Object
            (
                [data] => stdClass Object
                    (
                        [email] => veer@gmail.com
                        [first_name] => veer
                        [last_name] => 
                        [user_id] => 16
                        [owner_id] => 0
                        [optin_status] => 0
                        [date_created] => 2020-01-20 13:14:54
                        [ID] => 14
                        [gravatar] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
                        [age] => 
                    )

                [meta] => stdClass Object
                    (
                        [preferences_changed] => 1579526094
                        [user_login] => veer
                        [profile_picture] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
                        [birthday] => 
                    )

                [tags] => Array
                    (
                        [0] => 92
                        [1] => 13
                        [2] => 12
                        [3] => 9
                    )

                [files] => Array
                    (
                    )

            )

        [9] => stdClass Object
            (
                [data] => stdClass Object
                    (
                        [email] => singh.pratibha1432@gmail.com
                        [first_name] => Pratibha
                        [last_name] => Singh
                        [user_id] => 8
                        [owner_id] => 0
                        [optin_status] => 0
                        [date_created] => 2020-01-20 13:14:54
                        [ID] => 9
                        [gravatar] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
                        [age] => 
                    )

                [meta] => stdClass Object
                    (
                        [preferences_changed] => 1579526094
                        [user_login] => pratibha
                        [profile_picture] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
                        [birthday] => 
                    )

                [tags] => Array
                    (
                        [0] => 94
                        [1] => 93
                        [2] => 92
                        [3] => 91
                        [4] => 82
                        [5] => 15
                        [6] => 14
                        [7] => 13
                        [8] => 9
                    )

                [files] => Array
                    (
                    )

            )

        [4] => stdClass Object
            (
                [data] => stdClass Object
                    (
                        [email] => singh.dev1432@gmail.com
                        [first_name] => Devesh
                        [last_name] => Singh
                        [user_id] => 7
                        [owner_id] => 0
                        [optin_status] => 0
                        [date_created] => 2020-01-20 13:14:54
                        [ID] => 4
                        [gravatar] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
                        [age] => 
                    )

                [meta] => stdClass Object
                    (
                        [preferences_changed] => 1579526094
                        [user_login] => devesh
                        [profile_picture] => http://localhost:8888/wordpress/wp-content/themes/wp_lms/assets/images/avatar.jpg
                        [birthday] => 
                    )

                [tags] => Array
                    (
                        [0] => 93
                        [1] => 92
                        [2] => 15
                        [3] => 12
                        [4] => 11
                        [5] => 9
                    )

                [files] => Array
                    (
                    )

            )

        [2] => stdClass Object
            (
                [data] => stdClass Object
                    (
                        [email] => admin@gmail.com
                        [first_name] => veronica
                        [last_name] => 
                        [user_id] => 1
                        [owner_id] => 0
                        [optin_status] => 0
                        [date_created] => 2020-01-20 13:14:54
                        [ID] => 2
                        [gravatar] => http://localhost:8888/wordpress/wp-content/uploads/avatars/1/5dc525d984494-bpfull.jpg
                        [age] => 
                    )

                [meta] => stdClass Object
                    (
                        [preferences_changed] => 1579526094
                        [user_login] => admin
                        [profile_picture] => http://localhost:8888/wordpress/wp-content/uploads/avatars/1/5dc525d984494-bpfull.jpg
                        [birthday] => 
                    )

                [tags] => Array
                    (
                        [0] => 94
                        [1] => 92
                        [2] => 15
                        [3] => 14
                        [4] => 13
                        [5] => 9
                    )

                [files] => Array
                    (
                    )

            )

    )

[status] => success
)

и теперь я пытаюсь получить "электронную почту" из контактов-> id-> data-> email, поэтому для этого я использую этот код. Я пытаюсь зациклить вещи, с помощью которых я могу получить электронную почту со всех присутствующих идентификаторов. и в разделе идентификаторов есть данные, а внутри данных - электронная почта, так как я могу получить все электронные письма со всех идентификаторов.

foreach ((Array)$body->contacts as $id => $values) {
            foreach ($values as $data => $value) {
                $emails =  array('email'=>$value->email);
                return $emails;
            }
        }

но он возвращает только отдельные данные:

 Array
(
  [email] => veer@gmail.com
)

я хочу получить всю электронную почту вот так:

 Array
 (
[0] => Array
    (
        [emails] => veer@gmail.com
    )

[1] => Array
    (
        [emails] => singh.pratibha1432@gmail.com
    )

[2] => Array
    (
        [emails] => singh.dev1432@gmail.com
    )
[3] => Array
    (
        [emails] => admin@gmail.com
    )

)

как я могу этого достичь?

Всего 1 ответ


Определите нулевой массив и при каждом запуске сохраняйте каждое электронное письмо в этой переменной.

$emails = array();
foreach ((Array)$body->contacts as $id => $values) {
            foreach ($values as $data => $value) {
                $emails[] =  array('emails'=>$contact->data->email;);

            }
        }
return $emails;

Есть идеи?

10000