How to use PHP in_array with associative array?

Here is a one liner you can use.

$isInArray = in_array(1, array_column($names, 'ID'));

$isInArray = a Boolean (true or false) defining whether the value given was found within the column requested of the array in question.

1 = The value that is being searched for within the array.

$names = The array in question.

'ID' = The column within the array where we are searching for the value of 1.


Try this..... You can use this function for any depth of the associated array. Just contraint to this function is that the key value would not be repeat any where in array.

<?php 
function is_in_array($array, $key, $key_value){
      $within_array = 'no';
      foreach( $array as $k=>$v ){
        if( is_array($v) ){
            $within_array = is_in_array($v, $key, $key_value);
            if( $within_array == 'yes' ){
                break;
            }
        } else {
                if( $v == $key_value && $k == $key ){
                        $within_array = 'yes';
                        break;
                }
        }
      }
      return $within_array;
}
$test = array(
                0=> array('ID'=>1, 'name'=>"Smith"), 
                1=> array('ID'=>2, 'name'=>"John")
        );
print_r(is_in_array($test, 'name', 'Smith'));
?>

In your case, I wonder if simply using isset() makes more sense, i.e.

isset($a[$key])