【问题标题】:PHP error : exception 'ErrorException' with message 'Illegal string offset 'id''PHP错误:异常'ErrorException'带有消息'非法字符串偏移'id''
【发布时间】:2015-12-09 22:03:26
【问题描述】:

我正在使用一个数组,但在上面遇到了这个错误,我正在使用一个看起来像这样的非常简单的数组,

    array (
       'Emails' =>
           array (
               0 =>
                   array (
                       'id' => 172,
                       'email' => 'sam@andrews.com',
                       'first_name' => 'Sam',
                       'last_name' => 'Andrews',
                       'display_name' => 'simonainley',
                       'initials' => 'SA',
                       'active' => 1,
                       'login_type' => 'normal',
                       'cost_visible' => 0,
                       'notification_frequency' => 'D',
                       'admin' => 1,
                       'pivot' =>
                       array (
                           'organisation_id' => 200,
                           'user_id' => 172,
                           'is_admin' => 1,
                       ),
                  ),
              1 =>
                  array (
                      'id' => 110,
                      'email' => 'mike@fish.com',
                      'first_name' => 'Mike',
                      'last_name' => 'Fish',
                      'display_name' => 'mikefish',
                      'initials' => 'MF',
                      'active' => 1,
                      'login_type' => 'normal',
                      'cost_visible' => 0,
                      'notification_frequency' => 'H',
                      'admin' => 1,
                      'pivot' =>
                      array (
                          'organisation_id' => 200,
                          'user_id' => 110,
                          'is_admin' => 1,
                      ),
                  ),
                  'notification' => 'A user changed the status of New SEA LTD Projectto <strong>completed</strong>.',
       ),
  )

这个数组被设置成一个变量,然后我循环遍历它,像这样,

foreach($data['emails'] as $email) {
        Log::info($email);
        $emailData['id'] = $email['id'];    
        $emailData['first_name'] = $email['first_name'];
        $emailData['last_name'] = $email['last_name'];
        $emailData['email'] = $email['email'];

        Log::info($emailData);
}

Log::info($email) 输出以下内容,

array (
  'id' => 110,
  'email' => 'mike@fish.com',
  'first_name' => 'Mike',
  'last_name' => 'Fish',
  'display_name' => 'mikefish',
  'initials' => 'MF',
  'active' => 1,
  'login_type' => 'normal',
  'cost_visible' => 0,
  'notification_frequency' => 'H',
  'admin' => 1,
  'pivot' =>
  array (
    'organisation_id' => 200,
    'user_id' => 110,
    'is_admin' => 1,
  ),
)

Log::info($emailData) 输出以下内容,

array (
    'id' => 110,
    'first_name' => 'Mike',
    'last_name' => 'Fish',
    'email' => 'mike@fish.com',
)

所以我可以在我的日志中看到“id”属性,为什么我会看到,

异常 'ErrorException' 带有消息'非法字符串偏移'id''

这个异常显然是由这条线触发的,

$emailData['id'] = $email['id'];

有什么想法吗?

【问题讨论】:

  • 异常是由哪一行代码触发的?
  • @bogdan 添加了一个编辑。
  • 您的数组中有第三个条目'notification' =&gt; 'A user changed...',它没有id
  • @KirkBeard 是对的。您应该在foreach 中放置一个条件来检查数组项是否是这样的数组:if (is_array($email)),因为notification 项只是一个字符串,不会通过条件。

标签: php arrays laravel


【解决方案1】:

您的数组中有第三个条目'notification' =&gt; 'A user changed...',它是一个字符串,因此没有id(也没有任何其他字段:emailfirst_name 等)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多