Let’s start with an example.
$array[‘0’] is equal to value1. $array[‘1’] is equal to value2. $array[‘3’] is equal to value1.
I need to write a function that checks if anything but a certain set of strings exists in an array.
If I allowed “value1” and “value2” to exist in an array, and I gave the function the array above, it would return true.
If I fed the function $array[‘0’] = ‘value3’ $array[‘1’] = ‘value2’, the function would return false because the array contains “value3” and only “value1” and “value2” are allowed in this example.
Does a built-in PHP function that does this exist? If not, how would I go about writing it?
Let’s start with an example. $array[‘0’] is equal to value1. $array[‘1’] is equal to
Share
This function will do what you’re looking for:
It simply creates a unique
$input_array, and computes the difference between that array and the$valuesarray. If there a no elements in that difference, then both arrays had the same elements, and the function will return true. If not, it will have one or more elements, which makes the function return false.You can see it working in the demo.