Laravel Associative Array非法字符串偏移量

时间:2016-10-27 14:53:54

标签: php laravel

我正在玩mailchimp api,需要访问一些数据。

这是来自api的数组,我需要循环播放。

array:19 [▼
      "id" => "123123123"
      "email_address" => "sam.vines@discworld.com"
      "unique_email_id" => "c9a3123326649c8"
      "email_type" => "html"
      "status" => "subscribed"
      "merge_fields" => array:2 [▼
        "FNAME" => "Sam"
        "LNAME" => "Vines"
      ]
      "stats" => array:2 [▼
        "avg_open_rate" => 0
        "avg_click_rate" => 0
      ]
      "ip_signup" => ""
      "timestamp_signup" => ""
      "ip_opt" => "12.123.12.12"
      "timestamp_opt" => "2016-10-27T13:53:02+00:00"
      "member_rating" => 2
      "last_changed" => "2016-10-27T13:53:02+00:00"
      "language" => ""
      "vip" => false
      "email_client" => ""
      "location" => array:6 [▶]
      "list_id" => "7698082412"
      "_links" => array:8 [▶]
]

在我的观点中,我循环使用:

@foreach($mailchimp as $key => $user)
  <ul>
    <li>{{$user[$key]}}</li>
  </ul>
@endforeach

抛出此异常:

Illegal string offset 'id'...

我不知道如何解决这个问题

2 个答案:

答案 0 :(得分:0)

你的循环应该是这样的:

@foreach($mailchimp as $key => $user)
 <ul>
   <li>{{$user}}</li>
 </ul>
@endforeach

<强>更新

要打印$mailchimp的值,您可以直接访问它们:

{{ $mailchimp['email_address'] }}
{{ $mailchimp['merge_fields']['FNAME'] }} {{ $mailchimp['merge_fields']['LNAME'] }}
{{ $mailchimp['stats']['avg_open_rate'] }}

答案 1 :(得分:0)

你可能想要更像这样的东西:

@foreach($mailchimp as $user)
    @foreach($user as $key => $value)
      <ul>
        <li>{{value}}</li>
      </ul>
    @endforeach
@endforeach

这将为每个用户提供一个无序列表及其值。