php中的 compare,PHP compare array

Comparing two arrays to have equal values (duplicated or not, type-juggling taking into account) can be done by using array_diff() into both directions:

!array_diff($a, $b) && !array_diff($b, $a);

This gives TRUE if both arrays have the same values (after type-juggling). FALSE otherwise. Examples:

function array_equal_values(array $a, array $b) {

return !array_diff($a, $b) && !array_diff($b, $a);

}

array_equal_values([1], []); # FALSE

array_equal_values([], [1]); # FALSE

array_equal_values(['1'], [1]); # TRUE

array_equal_values(['1'], [1, 1, '1']); # TRUE

As this example shows, array_diff leaves array keys out of the equation and does not care about the order of values either and neither about if values are duplicated or not.

If duplication has to make a difference, this becomes more tricky. As far as "simple" values are concerned (only string and integer values work), array_count_values() comes into play to gather information about which value is how often inside an array. This information can be easily compared with ==:

array_count_values($a) == array_count_values($b);

This gives TRUE if both arrays have the same values (after type-juggling) for the same amount of time. FALSE otherwise. Examples:

function array_equal_values(array $a, array $b) {

return array_count_values($a) == array_count_values($b);

}

array_equal_values([2, 1], [1, 2]); # TRUE

array_equal_values([2, 1, 2], [1, 2, 2]); # TRUE

array_equal_values(['2', '2'], [2, '2.0']); # FALSE

array_equal_values(['2.0', '2'], [2, '2.0']); # TRUE

This can be further optimized by comparing the count of the two arrays first which is relatively cheap and a quick test to tell most arrays apart when counting value duplication makes a difference.

These examples so far are partially limited to string and integer values and no strict comparison is possible with array_diff either. More dedicated for strict comparison is array_search. So values need to be counted and indexed so that they can be compared as just turning them into a key (as array_search does) won't make it.

This is a bit more work. However in the end the comparison is the same as earlier:

$count($a) == $count($b);

It's just $count that makes the difference:

$table = [];

$count = function (array $array) use (&$table) {

$exit = (bool)$table;

$result = [];

foreach ($array as $value) {

$key = array_search($value, $table, true);

if (FALSE !== $key) {

if (!isset($result[$key])) {

$result[$key] = 1;

} else {

$result[$key]++;

}

continue;

}

if ($exit) {

break;

}

$key = count($table);

$table[$key] = $value;

$result[$key] = 1;

}

return $result;

};

This keeps a table of values so that both arrays can use the same index. Also it's possible to exit early the first time in the second array a new value is experienced.

This function can also add the strict context by having an additional parameter. And by adding another additional parameter would then allow to enable looking for duplicates or not. The full example:

function array_equal_values(array $a, array $b, $strict = FALSE, $allow_duplicate_values = TRUE) {

$add = (int)!$allow_duplicate_values;

if ($add and count($a) !== count($b)) {

return FALSE;

}

$table = [];

$count = function (array $array) use (&$table, $add, $strict) {

$exit = (bool)$table;

$result = [];

foreach ($array as $value) {

$key = array_search($value, $table, $strict);

if (FALSE !== $key) {

if (!isset($result[$key])) {

$result[$key] = 1;

} else {

$result[$key] += $add;

}

continue;

}

if ($exit) {

break;

}

$key = count($table);

$table[$key] = $value;

$result[$key] = 1;

}

return $result;

};

return $count($a) == $count($b);

}

Usage Examples:

array_equal_values(['2.0', '2', 2], ['2', '2.0', 2], TRUE); # TRUE

array_equal_values(['2.0', '2', 2, 2], ['2', '2.0', 2], TRUE); # TRUE

array_equal_values(['2.0', '2', 2, 2], ['2', '2.0', 2], TRUE, FALSE); # FALSE

array_equal_values(['2'], ['2'], TRUE, FALSE); # TRUE

array_equal_values([2], ['2', 2]); # TRUE

array_equal_values([2], ['2', 2], FALSE); # TRUE

array_equal_values([2], ['2', 2], FALSE, TRUE); # FALSE

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值