【问题标题】:Extract values from multiple arrays从多个数组中提取值
【发布时间】:2016-10-19 22:58:02
【问题描述】:

我目前正在使用 Adob​​e Echosign API 来格式化表格中的一些数据。输出如下所示:

{
  "agreementId": "",
  "events": [
    {
      "actingUserEmail": "",
      "actingUserIpAddress": "",
      "date": "date",
      "description": "",
      "participantEmail": "",
      "type": "",
      "versionId": ""
    }
  ],
  "locale": "",
  "modifiable": false,
  "name": "",
  "nextParticipantSetInfos": [
    {
      "nextParticipantSetMemberInfos": [
        {
          "email": "",
          "waitingSince": "date"
        }
      ]
    }
  ],
  "participantSetInfos": [
    {
      "participantSetId": "",
      "participantSetMemberInfos": [
        {
          "email": "",
          "participantId": ""
        }
      ],
      "roles": [
        ""
      ],
      "status": ""
    }
  ],
  "status": "",
  "vaultingEnabled": false
}

我正在遍历多个协议,这会将它们分别输出为一个单独的数组。

这可能是一个非常基本的问题,但我将如何遍历每个数组并提取“participantEmail”、“name”和“status”值?

谢谢!

【问题讨论】:

    标签: php arrays loops echosign


    【解决方案1】:

    根据您使用的编程语言,有很多方法可以处理这个问题。在 JS 中,您可以将此 JSON 数组转换为 JS 对象数组,然后您可以使用 JS 处理访问它们。

    JS 的例子是:

    var input = 'yourJSONstring';
    var jsObject = JSON.parse(input);
    for(var foo in jsObject){
    	var name = foo.name;
        var participantEmail=  foo.events[0].participantEmail;
        var status = foo.participantEmail[0].status;
    }

    【讨论】:

      【解决方案2】:

      假设您有一系列与您提供的格式的协议,您可以执行以下操作:

      <?php
      
      $json = '[{
        "agreementId": "",
        "events": [
          {
            "actingUserEmail": "",
            "actingUserIpAddress": "",
            "date": "date",
            "description": "",
            "participantEmail": "an email",
            "type": "",
            "versionId": ""
          }
        ],
        "locale": "",
        "modifiable": false,
        "name": "a name",
        "nextParticipantSetInfos": [
          {
            "nextParticipantSetMemberInfos": [
              {
                "email": "",
                "waitingSince": "date"
              }
            ]
          }
        ],
        "participantSetInfos": [
          {
            "participantSetId": "",
            "participantSetMemberInfos": [
              {
                "email": "",
                "participantId": ""
              }
            ],
            "roles": [
              ""
            ],
            "status": "a status"
          }
        ],
        "status": "",
        "vaultingEnabled": false
      }]';
      
      $parsed = json_decode($json, true);
      
      $names = [];
      foreach($parsed as $agreement) {
      
          $names[] = $agreement['name'];
      
          $emails = []; 
          foreach($agreement['events'] as $event) {
              $emails[] = $event['participantEmail'];
          }
      
          $status = []; 
          foreach($agreement['participantSetInfos'] as $participant) {
              $status[] = $participant['status'];
          }
      
      }
      
      var_dump($names);
      var_dump($emails);
      var_dump($status);
      

      当然,您应该对空值等进行一些检查,但这只是为了给您一个想法。 由于您没有明确姓名、状态和电子邮件之间的关系,我只是将它们放在单独的数组中,但这不是通过一些数组处理无法解决的。

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多