关于数组的比较,请大神们帮忙看看。
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") ) ) );
上面两个数组的比较结果应该是一样的
只是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;}
换个算法
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;}
唠叨老大太牛了。。。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The method of using a foreach loop to remove duplicate elements from a PHP array is as follows: traverse the array, and if the element already exists and the current position is not the first occurrence, delete it. For example, if there are duplicate records in the database query results, you can use this method to remove them and obtain results without duplicate records.

Methods for deep copying arrays in PHP include: JSON encoding and decoding using json_decode and json_encode. Use array_map and clone to make deep copies of keys and values. Use serialize and unserialize for serialization and deserialization.

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values takes a relatively long time.

Multidimensional array sorting can be divided into single column sorting and nested sorting. Single column sorting can use the array_multisort() function to sort by columns; nested sorting requires a recursive function to traverse the array and sort it. Practical cases include sorting by product name and compound sorting by sales volume and price.

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.

The best practice for performing an array deep copy in PHP is to use json_decode(json_encode($arr)) to convert the array to a JSON string and then convert it back to an array. Use unserialize(serialize($arr)) to serialize the array to a string and then deserialize it to a new array. Use the RecursiveIteratorIterator to recursively traverse multidimensional arrays.

PHP is a commonly used server-side scripting language widely used in website development and data processing fields. In PHP, it is a very common requirement to sort the values in an array by size. By using the built-in sort function, you can easily sort arrays. The following will introduce how to use PHP to sort the values in an array by size, with specific code examples: 1. Sort the values in the array in ascending order:

PHP's array_group() function can be used to group an array by a specified key to find duplicate elements. This function works through the following steps: Use key_callback to specify the grouping key. Optionally use value_callback to determine grouping values. Count grouped elements and identify duplicates. Therefore, the array_group() function is very useful for finding and processing duplicate elements.
