Home > Backend Development > PHP Tutorial > 关于数组的比较,请大神们帮忙看看。

关于数组的比较,请大神们帮忙看看。

WBOY
Release: 2016-06-23 13:55:50
Original
1068 people have browsed it

PHP如何对多维数组进行比较?实现类似一维数组比较函数 array_diff的功能?
也就是认为 array(a,b,c)和 array(c,b,a) 是两个一样的数组?
我想比较的数组结构比较复杂,可能是 3~4个维度,举个例子

		$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")								)				)		);
Copy after login

上面两个数组的比较结果应该是一样的
只是array('name'=>'KB11113')和array('name'=>'KB11112')的位置对调了一下
array('name'=>'sougou输入法','time'=>'2013/7/4','version'=>'6.7.0.0747')和array('name'=>'QQ2013','version'=>'1.92.6970.0','time'=>"2013/7/4")对调了一下,并且搜狗输入法内的内容对调了一下,但是值都是没有改变的,有什么好的办法,解决这个问题么,PHP 什么时候会有集合的概念和相关操作函数呢?


回复讨论(解决方案)

能否实现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;}
Copy after login
Copy after login

换个算法

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;}
Copy after login

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;}
Copy after login
Copy after login


唠叨老大太牛了。。。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template