简体   繁体   English

使用PHP访问JSON元素内的数组

[英]Access array inside JSON element using PHP

How do I access an array nested inside a JSON element with PHP. 如何使用PHP访问嵌套在JSON元素中的数组。 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 ? 下面是我使用的代码(简化的例子),我可以访问from_email, html, subject很容易等,但我怎么会进入to电子邮件地址recipient@addresshere.co.uk Do I need some sort of foreach loop, on the basis that there could be more than one entry? 在可能存在多个条目的基础上,我是否需要某种foreach循环?

JSON 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 的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 谢谢

You could, as you suggest, loop through the array with foreach . 如您所建议,您可以使用foreach在数组中循环。 For example this would echo the "to" addresses: 例如,这将回显“收件人”地址:

$to_arr = $email->msg->to[0];
foreach($to_arr as $to) {
    echo 'TO = '.$to.'<br />';
}

Using true as second function parameter, objects will be converted into associative arrays. 使用true作为第二个函数参数,对象将被转换为关联数组。

$email = json_decode($emailp, true);

Then you can easily access any data from the array, even use foreach if needed. 然后,您可以轻松访问阵列中的任何数据,甚至在需要时使用foreach。

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

Resuling in 进入

Array ( [0] => Array ( [0] => recipient@addresshere.co.uk [1] => ) )

It depends on what you expect from the data. 这取决于您对数据的期望。 Since it's an array, it could have 0 or more elements in it. 由于它是一个数组,因此其中可以包含0个或多个元素。 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; 如果您希望将所有电子邮件都放在一个数组中,则将整个内容转换$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]);
}

You would access the to like this 你会进入to喜欢这个

$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 或者,如果你想循环的整体to构建你可以

foreach ($json->msg->to as $i=>$v) {
    foreach ($v as $ii=>$vv) {
        echo "ii = $ii - vv = $vv\n";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM