Skip to content
Advertisement

Check if an array contains a specific value?

I’d like to check if there is a value on an array like this:

JavaScript

I’ve read the check_value_new method is a better way to work with arrays, but I’m not used to work with it, how I should fix it?

Advertisement

Answer

PHP offers a function called in_array that checks if a value exists in a given array.

You can change your check_value_new function to include this:

JavaScript

If you’d like to, you could also make the function work without the foreach loop, like so

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement