从JSON获取foreach循环中的PHP变量

时间:2016-12-31 03:51:38

标签: php json variables foreach

请查看我正在使用的以下代码:

<?php

$head = '[ { "status": "active", "confirmed_opt_in": null, "account_id": 1738888, "fields": { "first_name": "Ash", "last_name": "Wright" }, "member_id": 262268888, "last_modified_at": "@D:2016-12-30T19:58:25", "member_status_id": "a", "plaintext_preferred": false, "email_error": null, "member_since": "@D:2014-05-05T16:01:21", "bounce_count": 0, "deleted_at": null, "email": "ashwright@gmail.com" } ]';

$data = json_decode($head,true); // the second param will cast it as array, default is obejct. See the linked docs of json_decode!
foreach($data as $item) {
    $id=$item['member_id'];
    $email=$item['email'];
    echo "ID: $id EMAIL: $email<br/>";
    // put your code here to write into DB or whatever you wanna do!
}

?>

除了$id$email变量之外,我还想从上面的JSON返回中捕获名字和姓氏,以便获得名字和姓氏的变量。有人可以请我这样做的语法吗?

2 个答案:

答案 0 :(得分:1)

你应该print_r你的json解码数组。名字和姓氏都在&#34;字段中#34;键。

echo "<pre>";
print_r($data);

<强>阵列:

Array
(
  [0] => Array
    (
        [status] => active
        [confirmed_opt_in] => 
        [account_id] => 1738888
        [fields] => Array
            (
                [first_name] => Ash
                [last_name] => Wright
            )

        [member_id] => 262268888
        [last_modified_at] => @D:2016-12-30T19:58:25
        [member_status_id] => a
        [plaintext_preferred] => 
        [email_error] => 
        [member_since] => @D:2014-05-05T16:01:21
        [bounce_count] => 0
        [deleted_at] => 
        [email] => ashwright@gmail.com
    )

 )

<强>解决方案:

$first_name = $item['fields']['first_name'];
$last_name = $item['fields']['last_name'];

echo "Name : ". $first_name . ' '.$last_name;

答案 1 :(得分:0)

你应该尝试两件事,这里是muliple数组 1)解码json

Array

(     [0] =&gt;排列         (             [status] =&gt;活性             [confirmed_opt_in] =&gt;             [account_id] =&gt; 1738888             [fields] =&gt;排列                 (                     [first_name] =&gt;灰                     [last_name] =&gt;赖特                 )

        [member_id] => 262268888
        [last_modified_at] => @D:2016-12-30T19:58:25
        [member_status_id] => a
        [plaintext_preferred] => 
        [email_error] => 
        [member_since] => @D:2014-05-05T16:01:21
        [bounce_count] => 0
        [deleted_at] => 
        [email] => ashwright@gmail.com
    )

  

使用for循环或foreach循环来获取多个数组值

foreach ($data as $key => $value) {
   $first_name = $value['fields']['first_name'];
   $last_name = $value['fields']['last_name'];  
}
  

你应该打印first_name和last_name

echo "Name : ".$first_name." ".$last_name;

2)在foreach循环中使用extract()

foreach ($data as $key => $value) {
    extract($value);
    $first_name = $fields['first_name'];
    $last_name = $fields['last_name'];
}
  

print first_name和last_name

echo "Name : ".$first_name." ".$last_name;
  

如果您需要直接打印first_name和last_name,那么您应该尝试这个。

foreach ($data as $key => $value) {
    extract($value);
    extract($fields);
    echo "Name : ".$first_name." ".$last_name;  
}