I'm trying to create a function to return true if all items in $array1 are in $array2, if not return false.
What I'm trying to do:
$array1 = ['1', '3', '9', '17'];
$array2 = ['1', '2', '3', '4', '9', '11', '12', '17'];
function checkArray($arr, $arr2) {
If all elements in $arr are in $arr2 return true
else return false
}
if (checkArray($array1, $array2) {
// Function returned true
} else {
// Function returned false
}
I can't think of how to do this! Help much appreciated.
SOLUTION:
function checkArray($needles, $haystack) {
$x = array_diff($needles, $haystack);
if (count($x) > 0) {
return false;
} else {
return true;
}
}
// Returns false because more needles were found than in haystack
checkArray([1,2,3,4,5], [1,2,3]);
// Returns true because all needles were found in haystack
checkArray([1,2,3], [1,2,3,4,5]);