将数组值与数组元素进行比较

问题描述:

I have an array

$check = ['a', 'b', 'c'];

which I want to check against another array so that the values of $check should match the keys in $actual

$actual = ['a' => 'one', 'b' => 'two', 'c' => 'three'];

I can't use array_diff() === [] since array diff works on comparing the values and in this case I want to compare the values of one array against the keys in another.

我有一个数组 p>

  $ check = ['a  ','b','c']; 
  code>  pre> 
 
 

我要检查另一个数组,以便 $ check code>的值 应与 $ actual code> p>

  $ actual = ['a'=>中的键匹配 '一','b'=>  'two','c'=>  'three']; 
  code>  pre> 
 
 

我无法使用 array_diff()=== [] code>,因为数组差异用于比较值 在这种情况下,我想比较一个数组的值与另一个数组的键。 p> div>

You can use array_keys();

<?php
$check = ['a', 'b', 'c'];
$actual = ['a' => 'one', 'b' => 'two', 'c' => 'three'];

$result = array_diff(array_keys($actual), $check);
print_r($result);

In this case array_diff returns a empty array because all keys are found