• 主页

# 再问多维数组比较问题

dreams1981 2009-07-25 04:21:48
``````
\$q[]   =   Array ('a' => '1', 'b' => '11');
\$q[]   =   Array ('a' => '2', 'b' => '22');
\$s[]   =   Array ('a' => '1', 'b' => '11');
\$s[]   =   Array ('a' => '2', 'b' => 'BB');
\$s[]   =   Array ('a' => '3', 'b' => 'CC' );
\$s[]   =   Array ('a' => '4', 'b' => '11');

\$d = array_diff(\$s,\$q);//知道不是这样写

Array ( [1] => Array ('a' => '2', 'b' => 'BB'),
[2] => Array ('a' => '3', 'b' => 'CC' ),
[3] => Array ('a' => '4', 'b' => '11'));

\$e = array_diff(\$s,\$q);//知道不是这样写

Array ( [1] => Array ('a' => '2', 'b' => '22'));
``````

======================================

...全文
30 点赞 收藏 2

2 条回复

dreams1981 2009-07-25
``````
\$q[]   =   Array ('a' => '1', 'b' => '11');
\$q[]   =   Array ('a' => '2', 'b' => '22');
\$s[]   =   Array ('a' => '1', 'b' => '11');
\$s[]   =   Array ('a' => '2', 'b' => 'BB');
\$s[]   =   Array ('a' => '3', 'b' => 'CC' );
\$s[]   =   Array ('a' => '4', 'b' => '11');

\$d = array_diff(\$s,\$q);//知道不是这样写

Array ( [1] => Array ('a' => '2', 'b' => 'BB'),
[2] => Array ('a' => '3', 'b' => 'CC' ),
[3] => Array ('a' => '4', 'b' => '11'));

\$e = array_diff(\$q,\$s);//知道不是这样写

Array ( [1] => Array ('a' => '2', 'b' => '22'));

``````

blueforyou 2009-07-25

``````
function myArrayDiff(\$s,\$q){
\$d=array();
foreach(\$q as \$qKey=>\$qValue){
foreach(\$s as \$sKey=>\$sValue){
if(\$sValue!=\$qValue){
if(!in_array(\$sValue,\$d)) \$d[]=\$sValue;
}else{
unset(\$s[\$sKey]);
}
}
}
return \$d;
}
print_r(myArrayDiff(\$s,\$q));
``````

2.1w+