1

Here is the contents of $result array:

Array (
   [ts] => (somenumber)
   [_id] => (theid)
   [state] => sent
   [subject] => Test message
   [email] => (some email)
   [tags] => Array ( )
   [opens] => 0
   [clicks] => 0
   [smtp_events] => Array ( )
   [resends] => Array ( )
   [sender] => (some email)
   [template] => (some template)
   [opens_detail] => Array ( )
   [clicks_detail] => Array ( )
) 

I want to access [state] => sent so that I can print the status, which is whether an email has been sent or not. Below is my code where I try to retrieve it with a foreach loop:

$result = $GLOBALS['mandrill']->messages->info($id);

// Get the status of the email
foreach ($result as $key => $message) {
    $status = $message['state'];
}

I get the error Notice: Undefined index: state.

What am I doing wrong?

3 Answers 3

1

To simplify:

$result = $GLOBALS['mandrill']->messages->info($id);

$status = $result['state'];
Sign up to request clarification or add additional context in comments.

Comments

1

When using foreach loop always remember $key is the content between the square bracket and $message is the value. Try this:

$result = $GLOBALS['mandrill']->messages->info($id);

// Get the status of the email
foreach ($result as $key => $message) {
    if($key == "status") $status = $message;
}

But foreach loop should not be used that way. You can simply use $status = $result['status'] for your purpose.

1 Comment

Thanks bro appreciate it
0

If you want to access only state do this:

$result['state']; //This code finds the first array and the index of state

echo $result;

2 Comments

You can upvote answers if it work or accept them. @Spidey
Sorry there's a wait period couldn't do it yet haha, thanks for your help I appreciate it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.