1

I am working with a an array and I get getting this error above, I working with a very simply array that looks like this,

    array (
       'Emails' =>
           array (
               0 =>
                   array (
                       'id' => 172,
                       'email' => '[email protected]',
                       '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' => '[email protected]',
                      '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>.',
       ),
  )

This array gets set into a variable, and then I loop through it, like this,

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);
}

The Log::info($email) outputs the following,

array (
  'id' => 110,
  'email' => '[email protected]',
  '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,
  ),
)

The Log::info($emailData) outputs the following,

array (
    'id' => 110,
    'first_name' => 'Mike',
    'last_name' => 'Fish',
    'email' => '[email protected]',
)

So I can see the 'id' attribute in my logs so why would i be seeing,

exception 'ErrorException' with message 'Illegal string offset 'id''

the exception is being triggered by this line apparently,

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

any ideas?

4
  • By what line of code is the exception triggered? Commented Dec 9, 2015 at 22:07
  • @bogdan added an edit. Commented Dec 9, 2015 at 22:12
  • 3
    You have a third entry in your array 'notification' => 'A user changed...' which does not have a an id. Commented Dec 9, 2015 at 22:13
  • 2
    @KirkBeard is right. You should place a condition in your foreach that checks if the array item is an array like so: if (is_array($email)), because the notification item is only a string and will not pass the condition. Commented Dec 9, 2015 at 22:16

1 Answer 1

3

You have a third entry in your array 'notification' => 'A user changed...' which is a string, and therefore does not have an id (nor does it have any of the other fields: email, first_name, etc).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.