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?
'notification' => 'A user changed...'which does not have a anid.foreachthat checks if the array item is an array like so:if (is_array($email)), because thenotificationitem is only a string and will not pass the condition.