按键比较两个PHP数组

时间:2022-10-24 22:20:10

What's the fastest way to compare if the keys of two arrays are equal?

如果两个数组的键相等,最快的比较方法是什么?

for eg.

array1:          array2:

'abc' => 46,     'abc' => 46,
'def' => 134,    'def' => 134,
'xyz' => 34,     'xyz' => 34, 

in this case result should be TRUE (same keys)

在这种情况下,结果应为TRUE(相同的键)

and:

array1:          array2:

'abc' => 46,     'abc' => 46,
'def' => 134,    'def' => 134,
'qwe' => 34,     'xyz' => 34, 
'xyz' => 34,    

result should be FALSE (some keys differ)

结果应为FALSE(某些键不同)

array_diff_key() returns a empty array...

array_diff_key()返回一个空数组......

3 个解决方案

#1


15  

Use array_diff_key, that is what it is for. As you said, it returns an empty array; that is what it is supposed to do.

使用array_diff_key,就是它的用途。如你所说,它返回一个空数组;这是应该做的。

Given array_diff_key($array1, $array2), it will return an empty array if all of array1's keys exist in array2. To make sure that the arrays are equal, you then need to make sure all of array2's keys exist in array1. If either call returns a non-empty array, you know your array keys aren't equal:

给定array_diff_key($ array1,$ array2),如果array2中存在所有array1的键,它将返回一个空数组。要确保数组相等,您需要确保array1中存在所有array2的键。如果任一调用返回非空数组,则表示您的数组键不相等:

function keys_are_equal($array1, $array2) {
  return !array_diff_key($array1, $array2) && !array_diff_key($array2, $array1);
}

#2


4  

Use array_keys to get array of keys and then use array_diff.

使用array_keys获取键数组,然后使用array_diff。

OR

Use array_diff_key directly.

直接使用array_diff_key。

#3


3  

How about using === instead? You know, the operator for equality?

如何使用===代替?你知道,运营商是否平等?

$array1 = array(
    'abc' => 46,
    'def' => 134,
    'xyz' => 34
);


$array2 = array(
    'abc' => 46,
    'def' => 134,
    'xyz' => 34,
);


var_dump( array_keys( $array1 ) === array_keys( $array2 ) );

#1


15  

Use array_diff_key, that is what it is for. As you said, it returns an empty array; that is what it is supposed to do.

使用array_diff_key,就是它的用途。如你所说,它返回一个空数组;这是应该做的。

Given array_diff_key($array1, $array2), it will return an empty array if all of array1's keys exist in array2. To make sure that the arrays are equal, you then need to make sure all of array2's keys exist in array1. If either call returns a non-empty array, you know your array keys aren't equal:

给定array_diff_key($ array1,$ array2),如果array2中存在所有array1的键,它将返回一个空数组。要确保数组相等,您需要确保array1中存在所有array2的键。如果任一调用返回非空数组,则表示您的数组键不相等:

function keys_are_equal($array1, $array2) {
  return !array_diff_key($array1, $array2) && !array_diff_key($array2, $array1);
}

#2


4  

Use array_keys to get array of keys and then use array_diff.

使用array_keys获取键数组,然后使用array_diff。

OR

Use array_diff_key directly.

直接使用array_diff_key。

#3


3  

How about using === instead? You know, the operator for equality?

如何使用===代替?你知道,运营商是否平等?

$array1 = array(
    'abc' => 46,
    'def' => 134,
    'xyz' => 34
);


$array2 = array(
    'abc' => 46,
    'def' => 134,
    'xyz' => 34,
);


var_dump( array_keys( $array1 ) === array_keys( $array2 ) );