0

How do I access an array nested inside a JSON element with PHP. Here is the code I'm using (simplified for the example), I can access from_email, html, subject etc. easily, but how would I access the to email address recipient@addresshere.co.uk? Do I need some sort of foreach loop, on the basis that there could be more than one entry?

JSON

{
    "event": "inbound", 
    "msg": {
        "dkim": {}, 
        "email": "myemail@addresshere.co.uk", 
        "from_email": "example.sender@mandrillapp.com", 
        "headers": {}, 
        "html": "This is an example inbound message.", 
        "raw_msg": "Received: from mail115.us4.mandrillapp.com", 
        "sender": null, 
        "spam_report": {}, 
        "spf": {}, 
        "subject": "This is an example webhook message", 
        "tags": [ ], 
        "template": null, 
        "text": "This is an example inbound message.", 
        "text_flowed": false, 
        "to": [
            [
                "recipient@addresshere.co.uk", 
                null
            ]
        ]
    }
}

PHP

$emailp = urldecode($_REQUEST['mandrill_events']); // The above JSON
$email = array_pop(json_decode($emailp));

$from_email = $email->msg->from_email; // From email
$html = $email->msg->html; // HTML Message
$subject = $email->msg->subject; // Subject
$to = ????

Thanks

Rob
  • 738
  • 3
  • 10
  • 23

4 Answers4

1

You would access the to like this

$email->msg->to[0][0];

If you want to loop around the whole array you could do

foreach ($email->msg->to[0] as $i=>$v) {
    echo "i = $i and v = $v\n";
}

Or if you wanted to loop the whole to structure you could

foreach ($json->msg->to as $i=>$v) {
    foreach ($v as $ii=>$vv) {
        echo "ii = $ii - vv = $vv\n";
    }
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

You could, as you suggest, loop through the array with foreach. For example this would echo the "to" addresses:

$to_arr = $email->msg->to[0];
foreach($to_arr as $to) {
    echo 'TO = '.$to.'<br />';
}
GluePear
  • 7,244
  • 20
  • 67
  • 120
0

Using true as second function parameter, objects will be converted into associative arrays.

$email = json_decode($emailp, true);

Then you can easily access any data from the array, even use foreach if needed.

print_r($email['msg']['to']);

Resuling in

Array ( [0] => Array ( [0] => recipient@addresshere.co.uk [1] => ) )
JungleZombie
  • 1,181
  • 11
  • 11
  • @RiggsFolly Associative arrays are faster than objects, and, personally for me, have better readability when working with. – JungleZombie Jul 24 '15 at 22:26
0

It depends on what you expect from the data. Since it's an array, it could have 0 or more elements in it. How do you plan to deal with all the cases? If you expect to only even have one, just take the first element. If you want all of the emails in an array then take the entire thing $to = $email->msg->to;.

If you're going to use this information to fire of actual emails, then yes, you do want to iterate over all the elements.

$msg = new Email();
$msg->setSubject($subject);
// more logic
foreach($recepients as $recepient) {
    // assuming element 0 is email and element 1 is name
    $msg->addRecepient($recepient[0], $recepient[1]);
}
Anonymous
  • 11,740
  • 3
  • 40
  • 50