Return to Snippet

Revision: 51489
at September 26, 2011 21:28 by FeN_X


Initial Code
<?php

//Recherche les différentes valeurs de $needle dans $haystack
//Si $allValues vaut false, la fonction retourne true lorsqu'au moins une valeur de $needle est trouvé dans haystack
//Si $allValues vaut true, toutes les valeurs de $needle doivent être dans haystack

function in_array_r($needle, $haystack, $allValues = false, $strict = false)
{
	$found = $flag = false;
	
	for ($i = 0, $n = sizeof($needle); $i < $n && !$flag; $i++)
	{
		$found = (in_array($needle[$i], $haystack, $strict));
		
		if (($allValues && !$found) || (!$allValues && $found))
			$flag = true;
	}
	
	return $found;
}


echo (in_array_r(array(1,3), array(2,4,5,6))) ? 1 : 0;
echo '<br>';
echo (in_array_r(array(1,3), array(1,2,4,5,6))) ? 1 : 0;
echo '<br>';
echo (in_array_r(array(1,3), array(2,3,4,5,6), true)) ? 1 : 0;
echo '<br>';
echo (in_array_r(array(1,3), array(1,2,3,4,5,6), true)) ? 1 : 0;
echo '<br>';

?>

Initial URL


Initial Description
Find one or all values of an array in another array.

Initial Title
Recursive in_array

Initial Tags
php

Initial Language
PHP