Michael
Michael

Reputation: 471

PHP Warning: in_array() expects parameter 2 to be array, string given in

I have gone through all of the other posts on here that I can find regarding this warning, but I cannot seem to correct it. Warning is PHP Warning: in_array() expects parameter 2 to be array, string given in....

The code:

$my_teachers = $student->my_teachers;
                
if (in_array($teacher_email, $my_teachers)) {

I understand that $my_teachers needs to be an array, but it is as far as I can tell. The value is created by creating an array, pushing the teacher's emails into the array, and saving it to MySQL. The MySQL database row shows as

a:2:{i:1;s:16:"[email protected]";i:2;s:18:"[email protected]";}

Is this actually storing as a String and I'm not realizing it? Either way, how do I get rid of the warning? The code still gives me the intended results, but the warning is filling up my error_log.

Upvotes: 0

Views: 1147

Answers (1)

Cristino
Cristino

Reputation: 311

Maybe you could add an extra check, like:

$my_teachers = gettype($student->my_teachers) === 'array' ? $student->my_teachers : unserialize($student->my_teachers);

Upvotes: 2

Related Questions