首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >打印嵌套的JSON数组值

打印嵌套的JSON数组值
EN

Stack Overflow用户
提问于 2022-12-04 02:37:22
回答 1查看 24关注 0票数 1

我一直在试图找到打印个人数据的方法,但似乎找不出我哪里出了问题。

我从这个开始,但没有结果。在此之前,我尝试过嵌套循环,但也一无所获。

代码语言:javascript
复制
$data = curl_exec($ch);
$d = json_decode($data, true);
foreach($d as $k=>$v){
echo $v['value']['displayName'];
}

然后我试了一下,只得到了一些结果。我不知道我这是怎么回事。

代码语言:javascript
复制
foreach(json_decode($data,true) as $d){
 foreach($d as $k=>$v){
  foreach($v as $kk=>$vv){
   echo $kk.$vv;
   }
  }
}

JSON看起来如下所示:

代码语言:javascript
复制
{
  "value": [
    {
      "id": "",
      "name": "",
      "etag": "",
      "type": "Microsoft.SecurityInsights/alertRules",
      "kind": "Scheduled",
      "properties": {
        "incidentConfiguration": {
          "createIncident": true,
          "groupingConfiguration": {
            "enabled": false,
            "reopenClosedIncident": false,
            "lookbackDuration": "PT5M",
            "matchingMethod": "AllEntities",
            "groupByEntities": [],
            "groupByAlertDetails": null,
            "groupByCustomDetails": null
          }
        },
        "entityMappings": [
          {
            "entityType": "Account",
            "fieldMappings": [
              {
                "identifier": "FullName",
                "columnName": "AccountCustomEntity"
              }
            ]
          },
          {
            "entityType": "IP",
            "fieldMappings": [
              {
                "identifier": "Address",
                "columnName": "IPCustomEntity"
              }
            ]
          }
        ],
        "queryFrequency": "P1D",
        "queryPeriod": "P1D",
        "triggerOperator": "GreaterThan",
        "triggerThreshold": 0,
        "severity": "Medium",
        "query": "",
        "suppressionDuration": "PT1H",
        "suppressionEnabled": false,
        "tactics": [
          "Reconnaissance",
          "Discovery"
        ],
        "displayName": "MFA disabled for a user",
        "enabled": true,
        "description": "Multi-Factor Authentication (MFA) helps prevent credential compromise. This alert identifies when an attempt has been made to diable MFA for a user ",
        "alertRuleTemplateName": null,
        "lastModifiedUtc": "2022-11-14T02:20:28.8027697Z"
      }
    },
...
...
...
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-12-04 02:55:32

下面是如何在不需要循环的情况下获得显示名称。注意,0是数组的键值,因为它没有名称。

我们从value开始,通过选择第一个数组0,将一个层移动得更深。现在我们需要选择properties,最后,我们可以从那里得到displayName

代码语言:javascript
复制
$displayName = $d["value"][0]["properties"]["displayName"];
echo($displayName);

/*
Here is a quick demonstration:

value:
{
    0:
    {
        ...
        properties:
        {
            ...
            displayName: [We made it!]
        }
    }

}

*/

这里有一篇很好的文章,详细地解释了这一点

How to decode multi-layers nested JSON String and display in PHP?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74672452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档