Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following objects with arrays and trying to get the value of email. How can this be done?

stdClass Object (
    [status] => stdClass Object ( 
        [info] => Unsubscribe List [code] => 200 
    ) 
    [data] => Array ( 
        [[email protected]] => stdClass Object ( 
            [date] => 2013-01-28 08:09:19 
            [origin] => 
        ) 
        [[email protected]] => stdClass Object ( 
            [date] => 2013-01-28 08:35:59 
            [origin] => 
        ) 
    )    
) 

Currently i can access the date values with the following foreach.

foreach ($fnret->data as $msg) {
    echo $msg->date;      // shows 2013-01-28 08:35:59
}
share|improve this question
foreach ($array as $key => $value) is the answer. The $key holds value of what is in [] brackets. [key] => value – Qwerty Mar 1 at 10:07
add comment (requires an account with 50 reputation)

1 Answer

up vote 2 down vote accepted

This one should work :

foreach ($fnret->data as $email => $msg) {
    // echo $msg->date;      // shows 2013-01-28 08:35:59
    echo $email;
}
share|improve this answer
Thanks alot Sorin ! it worked like a charm :-) – Hasan Nawaz Jan 29 at 15:00
Great to hear that :) – Sorin Trimbitas Jan 29 at 15:03
To clarify it a bit so it does not look like magic. It's simple foreach ($array as $key => $value) statement. Meaning, this would be the input $array = Array ("key" => "value", 0 => 42, "p1" => "another"); – Qwerty Mar 1 at 10:04
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.