使用PHP从JSON获取值

时间:2016-09-14 21:31:25

标签: php json

使用API​​令牌,我得到以下JSON:

   {"data": [
            {"person_id": {
                "name": "AAA",
                "email": [;
                  {
                    "label": "work",
                    "value": "aaa@xxxcompany.cpm",
                    "primary": true
                  }
                ],
                "phone": [
                  {
                    "label": "work",
                    "value": "05078945623",
                    "primary": true
                  }
                ]
              }
    }

我尝试了以下代码,但它没有工作

foreach ($object['data'] as $key => $value){

     echo $value['person_id']['email']['value'];
     echo $value['person_id']['phone']['value'];
}

如何获取电子邮件值:aaa@xxxcompany.com和电话价值:0507894562在PHP?

我收到了错误:

  

注意:未定义的索引:C:\ xampp \ htdocs \ pipedrive \ index.php中的值
  第44行

     

注意:未定义的索引:C:\ xampp \ htdocs \ pipedrive \ index.php中的值
  第45行

1 个答案:

答案 0 :(得分:2)

按照树下的记号:

foo =  { "data": [
 |         |     |    { "person_id":
 |         |     |          |        {
 |         |     |          |          "name": "AAA",
 |         |     |          |          "email":   [
 |         |     |          |          |          |    { "label": "work"
 |         |     |          |          |          |         |
 |         |     |          |          |          |         |
foo    ['data']  [0]  ['person_id']    ['email']  [0]  ['label']

所以

 JS:    foo.data[0].person_id.email[0].label        -> "work"
 PHP:   $foo->data[0]->person_id->email[0]->label   -> "work"

你忘记了那里的所有数组([]),只处理了对象({})。