时间:2021-07-01 10:21:17 帮助过:3人阅读
$arr1 = array( 'hwres'=>array( 'cpu'=>array('brief'=>'intel core II i7-2620M @2.7GHZ double'), 'nainboard'=>array('brief'=>'lenvo 4180RW2 intel QM67'), 'memory'=>array('brief'=>'samsang DDR3 1333MHz 4GB'), 'monitor'=>array('brief'=>'youda AUO223E 14'), 'netcard'=>array( array('type'=>'有线网卡','brief'=>'intel 82579LM Gigabit network connection'), array('type'=>'无线网卡','brief'=>'瑞昱 RTL8188CE wireless lan 802.11n PCI-E MIC') )), 'softres'=>array( 'os'=>'windows7 64 home', 'sp'=>'sp3', 'kb'=>array( array('name'=>'KB11111'), array('name'=>'KB11112'), array('name'=>'KB11113')), 'software'=>array( array('name'=>'QQ2013','version'=>'1.92.6970.0','time'=>"2013/7/4"), array('name'=>'sougou输入法','version'=>'6.7.0.0747','time'=>'2013/7/4') ) ) ); $arr2 = array( 'hwres'=>array( 'cpu'=>array('brief'=>'intel core II i7-2620M @2.7GHZ double'), 'nainboard'=>array('brief'=>'lenvo 4180RW2 intel QM67'), 'memory'=>array('brief'=>'samsang DDR3 1333MHz 4GB'), 'monitor'=>array('brief'=>'youda AUO223E 14'), 'netcard'=>array( array('type'=>'有线网卡','brief'=>'intel 82579LM Gigabit network connection'), array('type'=>'无线网卡','brief'=>'瑞昱 RTL8188CE wireless lan 802.11n PCI-E MIC') )), 'softres'=>array( 'os'=>'windows7 64 home', 'sp'=>'sp3', 'kb'=>array( array('name'=>'KB11111'), array('name'=>'KB11113'), array('name'=>'KB11112')), 'software'=>array( array('name'=>'sougou输入法','time'=>'2013/7/4','version'=>'6.7.0.0747'), array('name'=>'QQ2013','version'=>'1.92.6970.0','time'=>"2013/7/4") ) ) );
能否实现array_diff 类似的效果呢,如果一样就返回空数组,如果不一样,就返回差集
print_r(array_diff_recursive($arr1, $arr2));print_r(array_diff_recursive($arr2, $arr1));function array_diff_recursive($a, $b) { $res = array(); if(is_numeric(key($a))) { //如果是下标数组,处理上还不尽如人意 foreach($a as $s) { $r = false; foreach($b as $k=>$v) { if($s == $v) { unset($b[$k]); $r = true; break; } } if(! $r) $res[] = $s; } return $res; } foreach(array_diff(array_keys($a),array_keys($b)) as $k) $res[$k] = $a[$k]; if($res) return $res; foreach($a as $k=>$v) { if($v == $b[$k]) continue; if(is_array($v)) { if($t = call_user_func_array( __FUNCTION__, array($v, $b[$k]))) $res[$k] = $t; }else $res[$k] = $v; } return $res;}
换个算法
function array_diff_recursive($a, $b, &$res=array()) { if($a == $b) return $res = array(); $res = $a; if(is_array($a)) { foreach($a as $k=>$v) { if(is_numeric($k)) { foreach($b as $i=>$t) if($v == $t) unset($res[$k]); }else { if(isset($b[$k])) array_diff_recursive($a[$k], $b[$k], $res[$k]); } } } if(is_array($res)) foreach($res as $k=>$v) if($v == array()) unset($res[$k]); return $res;}
print_r(array_diff_recursive($arr1, $arr2));print_r(array_diff_recursive($arr2, $arr1));function array_diff_recursive($a, $b) { $res = array(); if(is_numeric(key($a))) { //如果是下标数组,处理上还不尽如人意 foreach($a as $s) { $r = false; foreach($b as $k=>$v) { if($s == $v) { unset($b[$k]); $r = true; break; } } if(! $r) $res[] = $s; } return $res; } foreach(array_diff(array_keys($a),array_keys($b)) as $k) $res[$k] = $a[$k]; if($res) return $res; foreach($a as $k=>$v) { if($v == $b[$k]) continue; if(is_array($v)) { if($t = call_user_func_array( __FUNCTION__, array($v, $b[$k]))) $res[$k] = $t; }else $res[$k] = $v; } return $res;}