In my code below, I want the PHP to look for “NUMBER” with a value of 2 and say in Boolean whether it exists, however it’s not working:
<?
$array[] = array('NUMBER' => 1, 'LETTER' => 'A');
$array[] = array('NUMBER' => 2, 'LETTER' => 'B');
$array[] = array('NUMBER' => 3, 'LETTER' => 'C');
echo (in_array(array('NUMBER' => 2), $array)) ? '1' : '0'; // (expected: 1; actual: 0)
?>
Can anyone tell me where I’m going wrong please? Thanks in advance.
in_array()` compares the given values with the values of the array. In your case every entry of the array has two values, but the given array only contains one, thus you cannot compare both this way. I don’t see a way around
Maybe (especially with php5.3) you can build something with
array_map()orarray_reduce(). For exampleor