0

i have data in json something like that:

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
)

and now I am trying to fetch "email" from the contacts->id->data->email so for that i am using this code. I am trying to loop the things by which i can fetch email from all the ids present. and in the ids section, there is data and inside data the email is present so how can i fetch all emails from all the ids.

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

but it return only single data:

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

i want to fetch all the email like that:

 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
    )

)

how can I achieve this?

  • 2
    `return` returns immediately when it's first hit, which is on the first loop iteration. You want to create an empty array `$emails` *before* the loop, add values to it *in* the loop, and `return` it *after* the loop! – deceze Feb 10 '20 at 12:38
  • yes right! but have another issue as you can see we have "data"/"meta"/"tags"/"files". so it returns blank for the meta/tags and for files i only want to fetch the email from data not from the meta/tags and file how can i achieve this? – Veronica 99 Feb 10 '20 at 13:09
  • 1
    `foreach ($body->contacts as $contact) $emails[] = $contact->data->email;`…!? – deceze Feb 10 '20 at 13:11

1 Answers1

1

Define null array and in foreach run store every email in that variable.

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

            }
        }
return $emails;
Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49
  • yes right! but have another issue as you can see we have "data"/"meta"/"tags"/"files". so it returns blank for the meta/tags and for files i only want to fetch the email from data not from the meta/tags and file how can i achieve this? – Veronica 99 Feb 10 '20 at 13:08
  • check edited answer. – Dilip Hirapara Feb 10 '20 at 13:12
  • no it's not working it's giving the outpu: Array ([0] => Array( [emails] => vibe3@gmail.com [meta_preferences_changed] => ) [1] => Array ([emails] => [meta_preferences_changed] => ) [2] => Array ( [emails] => [meta_preferences_changed] => ) [3] => Array ([emails] => [meta_preferences_changed] => ) [4] => Array ( [emails] => vibe2@gmail.com [meta_preferences_changed] => ) – Veronica 99 Feb 10 '20 at 13:53
  • what kind of output do expect? – Dilip Hirapara Feb 10 '20 at 14:40
  • 1
    thank for you effort i found the solution it's foreach ($body->contacts as $contact) $emails[] = $contact->data->email; it's give all the emails from the data. – Veronica 99 Feb 11 '20 at 05:39
  • I edited as your answer so in future it'll help to others :) – Dilip Hirapara Feb 11 '20 at 05:41