[Solved] PHP in array not working


The reason I can think of why this returns ERROR:

if (in_array(array('string', 'id'), array('string', 'id')))
{
    echo 'IN ARRAY';
}
else
{
    echo 'ERROR!';
}

is because PHP is checking each of the haystack’s values against the needle itself. Suppose you have 2 arrays like so:

$a = array('string', 'id');
$b = array('string', 'id');

$a and $b are the same right? If you feed these to in_array, $a being the needle and $b being the haystack, it checks is $b[0] equal to $a? False. But if you make the arrays like this and do in_array($a, $b):

$a = array('string', 'id');
$b = array(array('string', 'id'), 'id');

it will return true because $b[0] is equal to $a itself or wherever $a occurred within the haystack which is $b.

0

solved PHP in array not working