我有2个阵列
array:23 [▼
"cpe_mac" => "298639133839"
"bandwidth_max_up" => 30000
"bandwidth_max_down" => 50000
"filter_icmp_inbound" => true
"dmz_enabled" => false
"dmz_host" => "http:\/\/ddd.com"
"vlan_id" => 2
"dns" => array:2 [▶]
"xdns_mode" => 0
"cfprofileid" => 11111
"stub_response" => "0"
"acl_mode" => 1
"portal_url" => "http:\/\/portal.com"
"fullbandwidth_max_up" => 1000000
"fullbandwidth_max_down" => 2000000
"fullbandwidth_guaranty_up" => 300000
"fullbandwidth_guaranty_down" => 400000
"account_id" => 1000
"location_id" => 3333
"network_count" => 3
"group_name" => "test_group"
"vse_id" => 20
"firewall_enabled" => false
]
array:23 [▼
"cpe_mac" => "a0a1a2a3a4a5"
"bandwidth_max_up" => 300000
"bandwidth_max_down" => 500000
"filter_icmp_inbound" => true
"dmz_enabled" => false
"dmz_host" => "http] = \/\/ddd.com"
"vlan_id" => 2
"dns" => array:2 [▶]
"xdns_mode" => 0
"cfprofileid" => 11111
"stub_response" => ""
"acl_mode" => 1
"portal_url" => "http] = \/\/portal.com"
"fullbandwidth_max_up" => 1000000
"fullbandwidth_max_down" => 2000000
"fullbandwidth_guaranty_up" => 300000
"fullbandwidth_guaranty_down" => 400000
"account_id" => 1234
"location_id" => 3333
"network_count" => 3
"group_name" => "test_group"
"vse_id" => 20
"firewall_enabled" => false
]
我用无数次的眼神看着他们,他们看起来和我一样除了他们的价值。然后,我以编程方式比较它们,
$equal = ($cpe == $sample );
dd($equal); // false
我认为,它返回false
因为它们的值不同。我对吗 ?
如何检查这些数组是否具有相同的
length
key
data-type
如果不同,我怎样才能找出使它们与众不同的具体内容?
答案 0 :(得分:1)
我不确定你是如何相信这些数组是平等的。他们显然不是。
例如$sample["cpe_mac"] == "a0a1a2a3a4a5"
和$cpe["cpe_mac"] == "298639133839"
。还有$sample["bandwidth_max_up"] == 300000
和$cpe["bandwidth_max_up"] == 30000
。只要查看数组就可以立即告诉它们不相等,所以没有合理的理由相信$cpe == $sample
应该是true
。
Also array_diff
compares values as strings。你拥有的是一个多维数组。因此,无法使用array_diff
计算数组中非标量元素的差异。
注意:
此函数仅检查n维数组的一维。当然,您可以使用
array_diff($array1[0], $array2[0]);
检查更深层次的维度。
该手册还阐述了第二个注释......
注意:
当且仅
if (string) $elem1 === (string) $elem2
时,两个元素被认为是相等的。用文字表示:当字符串表示相同时。
要计算两个数组之间的非相等元素,您可能需要迭代它们并一次比较一个元素。
$diff = [];
foreach($cpe as $key => $value) {
if (array_key_exists($key, $sample) && $sample[$key] !== $value) {
$diff[$key] = $sample[$key];
}
}
你也可以把它写成递归函数来计算n维数组中的差异......
function array_diff_recursive(Array $a, Array $b, Array $diff = [])
{
foreach($a as $k => $v) {
if (!array_key_exists($k, $b)) {
$diff[$k] = $v;
continue;
}
if (is_array($v)) {
$diff += array_diff_recursive($v, $b[$k], $diff);
} else {
if ($v !== $b[$k]) { // you could change this to == for loose comparison
$diff[$k] = $v;
}
}
}
return $diff;
}
答案 1 :(得分:0)
array_diff需要第一个参数要比较的数组和第二个参数要与进行比较的数组,并且将返回一个包含Array中条目的数组以进行比较来自的任何其他数组中都没有
$arr1 = array('first', 'second', 'third');
$arr2 = array('first', 'second', 'third', 'fourth', 'fifth');
print_r(array_diff($arr2, $arr1)); //Array ( [3] => fourth [4] => fifth )
但如果更改数组位置
$arr1 = array('first', 'second', 'third');
$arr2 = array('first', 'second', 'third', 'fourth', 'fifth');
print_r(array_diff($arr1, $arr2)); //Array ( )
答案 2 :(得分:0)
我会比较这个数组的密钥..
if (empty(array_diff_key($arr, $sample) ) ) {
echo "they have the same key";
}
虽然我应该提到55
与"55"
相同,但这仅适用于整数的数组键