首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP中JSON多维数组的访问值

PHP中JSON多维数组的访问值
EN

Stack Overflow用户
提问于 2018-07-18 08:43:00
回答 1查看 151关注 0票数 0

我尝试使用这两种方法来解析下面的代码($var

代码语言:javascript
复制
$convert = json_encode($var);
$object  = (object)json_decode($convert);
$array   = json_decode($convert, TRUE);

但是似乎无法通过$array->messages->event这样的东西访问值(通常是什么也看不到/空)。

我一直在交叉引用https://gist.githubusercontent.com/eurica/6034108/raw/10e7fbce580e515db0f0b74d49c812da4e5ce40b/PagerDutyWebhookToEmail.php上的一些示例代码。然而,它似乎是过时的(v1有效负载而不是v2)。

我的目标是在不同的级别拉取不同的值,以创建自定义电子邮件。文档显示"message“下可能有多个”message“,因此foreach可能是必要的?

外部参照https://community.pagerduty.com/t/how-do-i-notify-a-distribution-list-or-shared-group-email-address/1519https://v2.developer.pagerduty.com/docs/webhooks-v2-overview

代码语言:javascript
复制
{
"messages": [{
  "event": "incident.trigger",
  "log_entries": [
    {
      "id": "R2XGXEI3W0FHMSDXHDIBQGBQ5E",
      "type": "trigger_log_entry",
      "summary": "Triggered through the website",
      "self": "https://api.pagerduty.com/log_entries/R2XGXEI3W0FHMSDXHDIBQGBQ5E",
      "html_url": "https://webdemo.pagerduty.com/incidents/PRORDTY/log_entries/R2XGXEI3W0FHMSDXHDIBQGBQ5E",
      "created_at": "2017-09-26T15:14:36Z",
      "agent": {
        "id": "P553OPV",
        "type": "user_reference",
        "summary": "Laura Haley",
        "self": "https://api.pagerduty.com/users/P553OPV",
        "html_url": "https://webdemo.pagerduty.com/users/P553OPV"
      },
      "channel": {
        "type": "web_trigger",
        "summary": "My new incident",
        "subject": "My new incident",
        "details": "Oh my gosh"
      },
      "service": {
        "id": "PN49J75",
        "type": "service_reference",
        "summary": "Production XDB Cluster",
        "self": "https://api.pagerduty.com/services/PN49J75",
        "html_url": "https://webdemo.pagerduty.com/services/PN49J75"
      },
      "incident": {
        "id": "PRORDTY",
        "type": "incident_reference",
        "summary": "[#33] My new incident",
        "self": "https://api.pagerduty.com/incidents/PRORDTY",
        "html_url": "https://webdemo.pagerduty.com/incidents/PRORDTY"
      },
      "teams": [
        {
          "id": "P4SI59S",
          "type": "team_reference",
          "summary": "Engineering",
          "self": "https://api.pagerduty.com/teams/P4SI59S",
          "html_url": "https://webdemo.pagerduty.com/teams/P4SI59S"
        }
      ],
      "contexts": [],
      "event_details": {
        "description": "My new incident"
      }
    }
  ],
  "webhook": {
    "endpoint_url": "https://requestb.in/18ao6fs1",
    "name": "V2 wabhook",
    "description": null,
    "webhook_object": {
      "id": "PN49J75",
      "type": "service_reference",
      "summary": "Production XDB Cluster",
      "self": "https://api.pagerduty.com/services/PN49J75",
      "html_url": "https://webdemo.pagerduty.com/services/PN49J75"
    },
    "config": {},
    "outbound_integration": {
      "id": "PJFWPEP",
      "type": "outbound_integration_reference",
      "summary": "Generic V2 Webhook",
      "self": "https://api.pagerduty.com/outbound_integrations/PJFWPEP",
      "html_url": null
    },
    "accounts_addon": null,
    "id": "PKT9NNX",
    "type": "webhook",
    "summary": "V2 wabhook",
    "self": "https://api.pagerduty.com/webhooks/PKT9NNX",
    "html_url": null
  },
  "incident": {
    "incident_number": 33,
    "title": "My new incident",
    "description": "My new incident",
    "created_at": "2017-09-26T15:14:36Z",
    "status": "triggered",
    "pending_actions": [
      {
        "type": "escalate",
        "at": "2017-09-26T15:44:36Z"
      },
      {
        "type": "resolve",
        "at": "2017-09-26T19:14:36Z"
      }
    ],
    "incident_key": "56a1c5594fe340408230783eebf43be6",
    "service": {
      "id": "PN49J75",
      "name": "Production XDB Cluster",
      "description": "This service was created during onboarding on July 5, 2017.",
      "auto_resolve_timeout": 14400,
      "acknowledgement_timeout": 1800,
      "created_at": "2017-07-05T17:33:09Z",
      "status": "critical",
      "last_incident_timestamp": "2017-09-26T15:14:36Z",
      "teams": [
        {
          "id": "P4SI59S",
          "type": "team_reference",
          "summary": "Engineering",
          "self": "https://api.pagerduty.com/teams/P4SI59S",
          "html_url": "https://webdemo.pagerduty.com/teams/P4SI59S"
        }
      ],
      "incident_urgency_rule": {
        "type": "constant",
        "urgency": "high"
      },
      "scheduled_actions": [],
      "support_hours": null,
      "escalation_policy": {
        "id": "PINYWEF",
        "type": "escalation_policy_reference",
        "summary": "Default",
        "self": "https://api.pagerduty.com/escalation_policies/PINYWEF",
        "html_url": "https://webdemo.pagerduty.com/escalation_policies/PINYWEF"
      },
      "addons": [],
      "privilege": null,
      "alert_creation": "create_alerts_and_incidents",
      "integrations": [
        {
          "id": "PUAYF96",
          "type": "generic_events_api_inbound_integration_reference",
          "summary": "API",
          "self": "https://api.pagerduty.com/services/PN49J75/integrations/PUAYF96",
          "html_url": "https://webdemo.pagerduty.com/services/PN49J75/integrations/PUAYF96"
        },
        {
          "id": "P90GZUH",
          "type": "generic_email_inbound_integration_reference",
          "summary": "Email",
          "self": "https://api.pagerduty.com/services/PN49J75/integrations/P90GZUH",
          "html_url": "https://webdemo.pagerduty.com/services/PN49J75/integrations/P90GZUH"
        }
      ],
      "metadata": {},
      "type": "service",
      "summary": "Production XDB Cluster",
      "self": "https://api.pagerduty.com/services/PN49J75",
      "html_url": "https://webdemo.pagerduty.com/services/PN49J75"
    },
    "assignments": [
      {
        "at": "2017-09-26T15:14:36Z",
        "assignee": {
          "id": "P553OPV",
          "type": "user_reference",
          "summary": "Laura Haley",
          "self": "https://api.pagerduty.com/users/P553OPV",
          "html_url": "https://webdemo.pagerduty.com/users/P553OPV"
        }
      }
    ],
    "acknowledgements": [],
    "last_status_change_at": "2017-09-26T15:14:36Z",
    "last_status_change_by": {
      "id": "PN49J75",
      "type": "service_reference",
      "summary": "Production XDB Cluster",
      "self": "https://api.pagerduty.com/services/PN49J75",
      "html_url": "https://webdemo.pagerduty.com/services/PN49J75"
    },
    "first_trigger_log_entry": {
      "id": "R2XGXEI3W0FHMSDXHDIBQGBQ5E",
      "type": "trigger_log_entry_reference",
      "summary": "Triggered through the website",
      "self": "https://api.pagerduty.com/log_entries/R2XGXEI3W0FHMSDXHDIBQGBQ5E",
      "html_url": "https://webdemo.pagerduty.com/incidents/PRORDTY/log_entries/R2XGXEI3W0FHMSDXHDIBQGBQ5E"
    },
    "escalation_policy": {
      "id": "PINYWEF",
      "type": "escalation_policy_reference",
      "summary": "Default",
      "self": "https://api.pagerduty.com/escalation_policies/PINYWEF",
      "html_url": "https://webdemo.pagerduty.com/escalation_policies/PINYWEF"
    },
    "privilege": null,
    "teams": [
      {
        "id": "P4SI59S",
        "type": "team_reference",
        "summary": "Engineering",
        "self": "https://api.pagerduty.com/teams/P4SI59S",
        "html_url": "https://webdemo.pagerduty.com/teams/P4SI59S"
      }
    ],
    "alert_counts": {
      "all": 0,
      "triggered": 0,
      "resolved": 0
    },
    "impacted_services": [
      {
        "id": "PN49J75",
        "type": "service_reference",
        "summary": "Production XDB Cluster",
        "self": "https://api.pagerduty.com/services/PN49J75",
        "html_url": "https://webdemo.pagerduty.com/services/PN49J75"
      }
    ],
    "is_mergeable": true,
    "basic_alert_grouping": null,
    "alert_grouping": null,
    "metadata": {},
    "external_references": [],
    "importance": null,
    "incidents_responders": [],
    "responder_requests": [],
    "subscriber_requests": [],
    "urgency": "high",
    "id": "PRORDTY",
    "type": "incident",
    "summary": "[#33] My new incident",
    "self": "https://api.pagerduty.com/incidents/PRORDTY",
    "html_url": "https://webdemo.pagerduty.com/incidents/PRORDTY"
  },
  "id": "69a7ced0-a2cd-11e7-a799-22000a15839c",
  "created_on": "2017-09-26T15:14:36Z"
}
]}

代码语言:javascript
复制
echo $object->messages[0]->event;
Notice: Undefined property: stdClass::$messages in 
/var/www/pagerduty/public_html/wh/index.php on line 251

echo $array->messages[0]->event;
Notice: Trying to get property of non-object in 
/var/www/pagerduty/public_html/wh/index.php on line 255

echo $array['messages'][0]['event']
Warning: Illegal string offset 'messages' in 
/var/www/pagerduty/public_html/wh/index.php on line 258
Warning: Illegal string offset 'event' in 
/var/www/pagerduty/public_html/wh/index.php on line 258
{

print_r($array);
/*
* 提示:该行代码过长,系统自动注释不进行高亮。一键复制会移除系统注释 
* { "messages": [ { "event": "incident.trigger", "log_entries": [ { "id": "R2XGXEI3W0FHMSDXHDIBQGBQ5E", "type": "trigger_log_entry", "summary": "Triggered through the website", "self": "https://api.pagerduty.com/log_entries/R2XGXEI3W0FHMSDXHDIBQGBQ5E", "html_url": "https://webdemo.pagerduty.com/incidents/PRORDTY/log_entries/R2XGXEI3W0FHMSDXHDIBQGBQ5E", "created_at": "2017-09-26T15:14:36Z", "agent": { "id": "P553OPV", "type": "user_reference", "summary": "Laura Haley", "self": "https://api.pagerduty.com/users/P553OPV", "html_url": "https://webdemo.pagerduty.com/users/P553OPV" }, "channel": { "type": "web_trigger", "summary": "My new incident", "subject": "My new incident", "details": "Oh my gosh" }, "service": { "id": "PN49J75", "type": "service_reference", "summary": "Production XDB Cluster", "self": "https://api.pagerduty.com/services/PN49J75", "html_url": "https://webdemo.pagerduty.com/services/PN49J75" }, "incident": { "id": "PRORDTY", "type": "incident_reference", "summary": "[#33] My new incident", "self": "https://api.pagerduty.com/incidents/PRORDTY", "html_url": "https://webdemo.pagerduty.com/incidents/PRORDTY" }, "teams": [ { "id": "P4SI59S", "type": "team_reference", "summary": "Engineering", "self": "https://api.pagerduty.com/teams/P4SI59S", "html_url": "https://webdemo.pagerduty.com/teams/P4SI59S" } ], "contexts": [], "event_details": { "description": "My new incident" } } ], "webhook": { "endpoint_url": "https://requestb.in/18ao6fs1", "name": "V2 wabhook", "description": null, "webhook_object": { "id": "PN49J75", "type": "service_reference", "summary": "Production XDB Cluster", "self": "https://api.pagerduty.com/services/PN49J75", "html_url": "https://webdemo.pagerduty.com/services/PN49J75" }, "config": {}, "outbound_integration": { "id": "PJFWPEP", "type": "outbound_integration_reference", "summary": "Generic V2 Webhook", "self": "https://api.pagerduty.com/outbound_integrations/PJFWPEP", "html_url": null }, "accounts_addon": null, "id": "PKT9NNX", "type": "webhook", "summary": "V2 wabhook", "self": "https://api.pagerduty.com/webhooks/PKT9NNX", "html_url": null }, "incident": { "incident_number": 33, "title": "My new incident", "description": "My new incident", "created_at": "2017-09-26T15:14:36Z", "status": "triggered", "pending_actions": [ { "type": "escalate", "at": "2017-09-26T15:44:36Z" }, { "type": "resolve", "at": "2017-09-26T19:14:36Z" } ], "incident_key": "56a1c5594fe340408230783eebf43be6", "service": { "id": "PN49J75", "name": "Production XDB Cluster", "description": "This service was created during onboarding on July 5, 2017.", "auto_resolve_timeout": 14400, "acknowledgement_timeout": 1800, "created_at": "2017-07-05T17:33:09Z", "status": "critical", "last_incident_timestamp": "2017-09-26T15:14:36Z", "teams": [ { "id": "P4SI59S", "type": "team_reference", "summary": "Engineering", "self": "https://api.pagerduty.com/teams/P4SI59S", "html_url": "https://webdemo.pagerduty.com/teams/P4SI59S" } ], "incident_urgency_rule": { "type": "constant", "urgency": "high" }, "scheduled_actions": [], "support_hours": null, "escalation_policy": { "id": "PINYWEF", "type": "escalation_policy_reference", "summary": "Default", "self": "https://api.pagerduty.com/escalation_policies/PINYWEF", "html_url": "https://webdemo.pagerduty.com/escalation_policies/PINYWEF" }, "addons": [], "privilege": null, "alert_creation": "create_alerts_and_incidents", "integrations": [ { "id": "PUAYF96", "type": "generic_events_api_inbound_integration_reference", "summary": "API", "self": "https://api.pagerduty.com/services/PN49J75/integrations/PUAYF96", "html_url": "https://webdemo.pagerduty.com/services/PN49J75/integrations/PUAYF96" }, { "id": "P90GZUH", "type": "generic_email_inbound_integration_reference", "summary": "Email", "self": "https://api.pagerduty.com/services/PN49J75/integrations/P90GZUH", "html_url": "https://webdemo.pagerduty.com/services/PN49J75/integrations/P90GZUH" } ], "metadata": {}, "type": "service", "summary": "Production XDB Cluster", "self": "https://api.pagerduty.com/services/PN49J75", "html_url": "https://webdemo.pagerduty.com/services/PN49J75" }, "assignments": [ { "at": "2017-09-26T15:14:36Z", "assignee": { "id": "P553OPV", "type": "user_reference", "summary": "Laura Haley", "self": "https://api.pagerduty.com/users/P553OPV", "html_url": "https://webdemo.pagerduty.com/users/P553OPV" } } ], "acknowledgements": [], "last_status_change_at": "2017-09-26T15:14:36Z", "last_status_change_by": { "id": "PN49J75", "type": "service_reference", "summary": "Production XDB Cluster", "self": "https://api.pagerduty.com/services/PN49J75", "html_url": "https://webdemo.pagerduty.com/services/PN49J75" }, "first_trigger_log_entry": { "id": "R2XGXEI3W0FHMSDXHDIBQGBQ5E", "type": "trigger_log_entry_reference", "summary": "Triggered through the website", "self": "https://api.pagerduty.com/log_entries/R2XGXEI3W0FHMSDXHDIBQGBQ5E", "html_url": "https://webdemo.pagerduty.com/incidents/PRORDTY/log_entries/R2XGXEI3W0FHMSDXHDIBQGBQ5E" }, "escalation_policy": { "id": "PINYWEF", "type": "escalation_policy_reference", "summary": "Default", "self": "https://api.pagerduty.com/escalation_policies/PINYWEF", "html_url": "https://webdemo.pagerduty.com/escalation_policies/PINYWEF" }, "privilege": null, "teams": [ { "id": "P4SI59S", "type": "team_reference", "summary": "Engineering", "self": "https://api.pagerduty.com/teams/P4SI59S", "html_url": "https://webdemo.pagerduty.com/teams/P4SI59S" } ], "alert_counts": { "all": 0, "triggered": 0, "resolved": 0 }, "impacted_services": [ { "id": "PN49J75", "type": "service_reference", "summary": "Production XDB Cluster", "self": "https://api.pagerduty.com/services/PN49J75", "html_url": "https://webdemo.pagerduty.com/services/PN49J75" } ], "is_mergeable": true, "basic_alert_grouping": null, "alert_grouping": null, "metadata": {}, "external_references": [], "importance": null, "incidents_responders": [], "responder_requests": [], "subscriber_requests": [], "urgency": "high", "id": "PRORDTY", "type": "incident", "summary": "[#33] My new incident", "self": "https://api.pagerduty.com/incidents/PRORDTY", "html_url": "https://webdemo.pagerduty.com/incidents/PRORDTY" }, "id": "69a7ced0-a2cd-11e7-a799-22000a15839c", "created_on": "2017-09-26T15:14:36Z" } ] }
*/
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51391970

复制
相关文章

相似问题

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