如何从PHP中的数组中选择等于某个特定值的键?

for example if i have the following array:

$numbers=array(
"A"=>$value1,
"B"=>$value2,
"C"=>$value3,
"D"=>$value4,
"E"=>$value5,
"F"=>$value6,
"G"=>$value7,
);

and if some of the value variables are equal to 0 and the rest are equal to 1, how can I select the keys which values are equal, for example to 0?

$result = [];

foreach($numbers as $id => $number) {

     if($number ==0)
         $result[$id] = $number;

}

Try this:

$all_zeros = array_filter($numbers);
$all_ones = array_diff($numbers, $all_zeros);

Also you might use a custom filter function like below:

function custom_filter($numbers, $targetValue) {
    return array_filter($numbers, function ($i) use ($targetValue) {
        return $targetValue == $i;
    });
}

Ref:

  1. array_filter
  2. array_intersect
  3. PHP Anonymous function