Skip to content

Instantly share code, notes, and snippets.

@pmgupte
Last active December 27, 2015 15:59
Show Gist options
  • Save pmgupte/7351189 to your computer and use it in GitHub Desktop.
Save pmgupte/7351189 to your computer and use it in GitHub Desktop.
PHP function to check whether all elements from one array are present in another array.
<?php
/**
* Check whether all elements from $needle array are present in $haystack array.
* Works for single-dimentional arrays only.
* If you use multi-dimentional arrays, result could be misterious.
* @param array $needle
* @param array $haystack
* @return boolean true if all elements in $needle are found in $haystack. false otherwise.
*/
function array_in_array($needle, $haystack) {
return (count($needle) == count(array_intersect($needle, $haystack)));
}
// example
$needle = array(1, 2, null, "abc", 4.5);
$haystack = array(null, null, 4, 5, "a", "b", "c", 6, 3, 8, 2, 1, 0, "bca", "abc", 9/2);
var_dump(array_in_array($needle, $haystack));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment