0

I have a json return that looks like this:

[output] => stdClass Object
    (
        [data] => stdClass Object
            (
                [Email] => Array
                    (
                        [0] => [email protected]
                        [1] => [email protected]
                    )

                [PhoneNumber] => Array
                    (
                        [0] => 2031234569
                    )

            )

What i need to do is be able to loop through the phones and emails and get them all.

I have tried:

foreach ($json_result->output->data as $data) {
$phone = $data->PhoneNumber;
$email = $data->Email;
}

but this is returning empty. Anyone have an idea?

1
  • Your sample code isn't "returning" anything, it's just a couple of variable assignments. What are you trying to do with $phone and $email? Commented Apr 19, 2016 at 16:49

2 Answers 2

1

You are trying to access PhoneNumber and Email as properties, while they are arrays.

foreach ($json_result->output->data->Email as $email_address) {
    echo $email_address;
}

foreach ($json_result->output->data->PhoneNumber as $phone_number) {
    echo $phone_number;
}
Sign up to request clarification or add additional context in comments.

1 Comment

PhoneNumber and Email are properties of the object. They themselves are arrays, but there was nothing wrong with his original code.
1

The easiest way is JSON encode your std object and then decode it back to an array:

$array = json_decode(json_encode($object), true);
foreach($array as $data) {
   $phone = $data->PhoneNumber;
   $email = $data->Email;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.