almost a solution:
@shalvah gave a good starting point. However, in the suggested solution he forgot to loop over the elements of the $needle array like shown below:
function array_in_array($neearr,$haystack) {
foreach ($neearr as $needle){
foreach ($haystack as $array) {
//check arrays for equality
if(count($needle) == count($array)) {
$needleString = serialize($needle);
$arrayString = serialize($array);
echo "$needleString||$arrayString<br>";
if(strcmp($needleString, $arrayString) == 0 ) return true;
}
return false;
}
}
}
But even so is this not completely "water tight". In cases where elements of the "needle" arrays appear in a different order (sequence) the serialze()-function will produce differing strings and will lead to false negatives, like shown in the exampe below:
$hay=array(array('a'=>'car','b'=>'bicycle'),
array('a'=>'bus','b'=>'truck'),
array('a'=>'train','b'=>'coach'));
$nee1=array(array('a'=>'car','b'=>'bicycle'),
array('a'=>'train','b'=>'coach'));
$nee2=array(array('b'=>'bicycle','a'=>'car'), // different order of elements!
array('a'=>'train','b'=>'coach'));
echo array_in_array($nee1,$hay); // true
echo array_in_array($nee2,$hay); // false (but should be true!)
a slightly better solution
This problem can be solved by first sorting (ksort(): sort by key value) all the elements of all the "needle" arrays before serialize-ing them:
function array_in_array($neearr,$haystack) {
$haystackstrarr = array_map(function($array){ksort($array);return serialize($array);},$haystack);
foreach ($neearr as $needle){
ksort($needle);
$needleString = serialize($needle);
foreach ($haystackstrarr as $arrayString){
if(strcmp($needleString, $arrayString) == 0 ) return true;
}
return false;
}
}
echo array_in_array($nee1,$hay); // true
echo array_in_array($nee2,$hay); // true
Needlehas constant key name?