0

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.

2
  • 2
    Seems it's a serialize() string. Use unserialize($string) to transform back into array. Commented Dec 16, 2020 at 15:33
  • Usually the frameworks have an built-in way to transform back to array/object. You may need to look at your project (wordpress or plugin) to see if there is a better way. Commented Dec 16, 2020 at 15:34

1 Answer 1

2

Maybe you could add an extra check, like:

$my_teachers = gettype($student->my_teachers) === 'array' ? $student->my_teachers : unserialize($student->my_teachers);
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.