列出JSON响应PHP中的值

时间:2019-06-19 02:03:22

标签: php json

我目前正在使用Amazon AWS SNS向客户发送短信。

我想简单地显示一个下拉菜单,其中每个topicArn作为每个选项。

我从var_dump($topics);收到的响应中尝试了以下方法,但是它不起作用。

<div class="form-group">
                <label for="exampleFormControlSelect1">Select list</label>
                <select class="form-control" name="selectList" id="selectList">
                   <?php
                    $topics = $sns->listTopics();
                    $test = $topics->get('Topics');
                     foreach($test['TopicArn'] as $topicArns)
                     {
                          echo '<option>'.$topicArns.'</option>';
                     }  
                  ?>
                </select>
              </div>

响应:

object(Aws\Result)#109(2){
  [
    "data": "Aws\Result": private
  ]=>array(2){
    [
      "Topics"
    ]=>array(4){
      [
        0
      ]=>array(1){
        [
          "TopicArn"
        ]=>string(50)"arn:aws:sns:eu-west-1:547872464065:Custoddmers-Optin"
      }[
        1
      ]=>array(1){
        [
          "TopicArn"
        ]=>string(42)"arn:aws:sns:eu-west-1:547872464065:Testing"
      }[
        2
      ]=>array(1){
        [
          "TopicArn"
        ]=>string(55)"arn:aws:sns:eu-west-1:547872464065:test19-06-2019_02-19"
      }[
        3
      ]=>array(1){
        [
          "TopicArn"
        ]=>string(57)"arn:aws:sns:eu-west-1:547872464065:tester19-06-2019_02-17"
      }
    }[
      "@metadata"
    ]=>array(4){
      [
        "statusCode"
      ]=>int(200)[
        "effectiveUri"
      ]=>string(35)"https://sns.eu-west-1.amazonaws.com"[
        "headers"
      ]=>array(4){
        [
          "x-amzn-requestid"
        ]=>string(36)"92f435gd-8fd5-5c0e-ad5hd-bhb74hkd74c66d"[
          "content-type"
        ]=>string(8)"text/xml"[
          "content-length"
        ]=>string(3)"717"[
          "date"
        ]=>string(29)"Wed, 19 Jun 2019 01:53:14 GMT"
      }[
        "transferStats"
      ]=>array(1){
        [
          "http"
        ]=>array(1){
          [
            0
          ]=>array(0){

          }
        }
      }
    }
  }[
    "monitoringEvents": "Aws\Result": private
  ]=>array(0){

  }
}

1 个答案:

答案 0 :(得分:0)

怎么样?

foreach($test['Topics'] as $topic)
{
 echo '<option>'.$topic['TopicArn'].'</option>';
}