我希望我能正确理解这个问题。如果您首先对数据进行规范化,则可以只比较数组
例如:
$a = array(
10 => 'net',
12 => 'evlewt',
22 => '10,12,11',
30 => '12,10'
);
$b = array(
10 => 'net',
12 => 'evlewt',
22 => '11,12,10',
30 => '12,10'
);
function normalize(&$value, $key) {
$value = explode(',', $value);
sort($value);
$value = implode(',', $value);
}
array_walk($a, 'normalize');
array_walk($b, 'normalize');
var_dump($a == $b); // outputs true
编辑:为了通过任一数组中的元素是否是另一个数组的子集来评估两个数组,我会将值标准化为数组并使用该reduce_array()
函数。
<?php
$a = array(
10 => 'net',
12 => 'evlew',
22 => '10,12,11',
30 => '12,10,11'
);
$b = array(
10 => 'net',
12 => 'evlewt',
22 => '11,12,10',
30 => '12,10'
);
function normalize(&$value, $key) {
$value = explode(',', $value);
}
function compare_value($v, $w) {
if (false === $v) return false;
global $a, $b;
if(is_subset($a[$w], $b[$w]) || is_subset($b[$w], $a[$w]))
return true;
return false;
}
function is_subset($needle, $haystack) {
return count(array_intersect($needle, $haystack)) === count($needle);
}
array_walk($a, 'normalize');
array_walk($b, 'normalize');
$result = array_reduce(array_keys($a), 'compare_value', true);
var_dump($result); // outputs false
$a = array(
10 => 'net',
12 => 'evlewt',
22 => '10',
30 => '12,10,11'
);
$b = array(
10 => 'net',
12 => 'evlewt',
22 => '11,12,10',
30 => '12,10'
);
array_walk($a, 'normalize');
array_walk($b, 'normalize');
$result = array_reduce(array_keys($a), 'compare_value', true);
var_dump($result); // outputs true