questions

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

Rate this post

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.

 

✔️Solution:

Maybe you could add an extra check, like:

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

Leave a Reply

Your email address will not be published.

Back to top button