PHP错误:异常' ErrorException'消息'非法字符串偏移' id''

时间:2015-12-09 22:03:26

标签: php arrays laravel

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

    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',
)

所以我可以看到&#39; id&#39;在我的日志属性中,为什么我会看到,

  

例外&#39; ErrorException&#39;有消息&#39;非法字符串偏移&#39; id&#39;&#39;

显然,这条线正在触发异常,

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

任何想法?

1 个答案:

答案 0 :(得分:3)

您的数组'notification' => 'A user changed...'中有第三个条目是字符串,因此没有id(也没有任何其他字段:email,{ {1}}等。)