【问题标题】:Get result out of nested Json with php使用 php 从嵌套的 Json 中获取结果
【发布时间】:2017-01-19 14:23:30
【问题描述】:

我想用 PHP 从嵌套的 JSON 文件中检索一个值。我创建了以下代码:

<?php
$string = file_get_contents("https://api.abc");
$json = json_decode($string,true);
foreach ($json['contacts'][0]['properties']['company'] as $person) {

  echo $person['value'];
}
?>

它只会给我 1 个结果,并且只有值的第一个字母。如何获得更多结果而不仅仅是第一个字母?

{
    "contacts": [{
        "addedAt": 1414182103652,
        "vid": 2,
        "canonical-vid": 2,
        "merged-vids": [],
        "portal-id": 448845,
        "is-contact": true,
        "properties": {
            "firstname": {
                "value": "Andre"
            },
            "lastmodifieddate": {
                "value": "1472024018755"
            },
            "company": {
                "value": "test"
            },
            "lastname": {
                "value": "Jansen"
            }
        },

【问题讨论】:

  • 看不到$json的内容我们什么也说不出来
  • 显示你的 json 数据。
  • 感谢您的快速响应,这里是 JSON 的快照:

标签: php json


【解决方案1】:

试试这个, 仅在 $person['value'] 的位置上使用 $person

$json = json_decode($string,true);
foreach ($json['contacts'][0]['properties']['company'] as $person) {
  echo $person;
}

DEMO

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-08
  • 1970-01-01
  • 2016-08-22
  • 2016-08-19
  • 2022-11-23
  • 2022-11-23
相关资源
最近更新 更多