array_diff问题 - PHP

时间:2013-10-15 00:45:00

标签: php

我有array1

    array(20) { [0]=> array(1) { [0]=> string(30) "0x0000000000000013620010713E5A" } 
    [1]=> array(1) { [0]=> string(30) "0x000000000000001077002942CE57" } 
    [2]=> array(1) { [0]=> string(30) "0x20120427850602000103026906B2" } 
    [3]=> array(1) { [0]=> string(30) "0xE20062969619018824701915D683" } 
    [4]=> array(1) { 
    [0]=> string(30) "0x0000000008002216572D06103CF6" } [5]=> array(1) { 
    [0]=> string(30) "0xE2004465650F015224401B255262" } [6]=> array(1) { 
    [0]=> string(30) "0xE20092012053100000002850D14E" } 
    [7]=> array(1) { [0]=> string(30) "0x300833B2DDD901400000000039BB" } 
    [8]=> array(1) { [0]=> string(30) "0xE2002996961802570960B3F06912" } 
    [9]=> array(1) { [0]=> string(30) "0x000000000000000000501420B8B6" } 
    [10]=> array(1) { 
    [0]=> string(30) "0x201203298478040001020252A5EC" } [11]=> array(1) { 
    [0]=> string(30) "0xE20010008007026819204FCAF906" } [12]=> array(1) { 
    [0]=> string(30) "0x0000000000000000000007485DF6" } [13]=> array(1) { 
    [0]=> string(30) "0xE2001036990F008812908EA5481C" } [14]=> array(1) { 
    [0]=> string(30) "0x0019000000000000000043B94C3A" } [15]=> array(1) { 
    [0]=> string(30) "0x0000000000000000004004490529" } [16]=> array(1) { 
    [0]=> string(30) "0x0000000000000000000010066E18" } [17]=> array(1) { 
    [0]=> string(30) "Error 0100:   syntax error at ''" } [18]=> array(1) { 
    [0]=> string(34) "Error 0102:   Error performing query" } [19]=> array(1) { 
    [0]=> NULL } }

array2

array(4) { [0]=> array(1) { [0]=> string(30) "0x0019000000000000000043B94C3A" } 
[1]=> array(1) { [0]=> string(30) "0x0000000000000000004004490529" } [2]=> array(1) { 
[0]=> string(30) "0x0000000000000000000010066E18" } 
[3]=> array(1) { [0]=> string(30) "0xE2004465650F015224401B255262" } } array(0) { }

现在,我使用array_diff()来检查array2中是否列出了series <{1}}

array1

array_diff()

我的问题是在检查$results = array_diff($devices_ForUser, $device_CurrentIDS); 是否为空时,如果不是,则为空。

建议和想法?

大卫

2 个答案:

答案 0 :(得分:1)

请参阅http://uk.php.net/manual/en/function.array-diff.php

  

此函数仅检查n维数组的一维。的   当然你可以使用array_diff($ array1 [0]来检查更深的维度,   $数组2 [0]);

你有一个嵌套数组,这个工作无法解决。

修改

这可能有效:

function array_values_recursive($ary)
{
   $lst = array();
   foreach( array_keys($ary) as $k ){
      $v = $ary[$k];
      if (is_scalar($v)) {
         $lst[] = $v;
      } elseif (is_array($v)) {
         $lst = array_merge( $lst,
            array_values_recursive($v)
         );
      }
   }
   return $lst;
}

$result = array_diff(array_values_recursive($devices_ForUser), array_values_recursive($device_CurrentIDS));
var_dump($result);

答案 1 :(得分:1)

您可以使用SPL执行您想要的操作。

$array_1=array( 
0=> array( 0=>  "0x0000000000000013620010713E5A" ),
1=> array( 0=>  "0x000000000000001077002942CE57" ),
2=> array( 0=>  "0x20120427850602000103026906B2" ) ,
3=> array( 0=>  "0xE20062969619018824701915D683" ),
4=> array( 0=>  "0x0000000008002216572D06103CF6" ) ,
5=> array( 0=>  "0xE2004465650F015224401B255262" ) ,
6=> array( 0=>  "0xE20092012053100000002850D14E" ) ,
7=> array( 0=>  "0x300833B2DDD901400000000039BB" ) ,
8=> array( 0=>  "0xE2002996961802570960B3F06912" ) ,
9=> array( 0=>  "0x000000000000000000501420B8B6" ) ,
10=> array(0=>  "0x201203298478040001020252A5EC" ) ,
11=> array( 0=>  "0xE20010008007026819204FCAF906" ) ,
12=> array( 0=>  "0x0000000000000000000007485DF6" ) ,
13=> array( 0=>  "0xE2001036990F008812908EA5481C" ) ,
14=> array( 0=>  "0x0019000000000000000043B94C3A" ) ,
15=> array( 0=>  "0x0000000000000000004004490529" ) ,
16=> array( 0=>  "0x0000000000000000000010066E18" ) ,
17=> array( 0=>  "Error 0100:   syntax error at ''" ),
18=> array( 0=>  "Error 0102:   Error performing query" ),
19=> array( 0=> NULL ) 
);


$array_2=array(
0=> array( 0=> "0x0019000000000000000043B94C3A" ),
1=> array( 0=> "0x0000000000000000004004490529" ) ,
2=> array( 0=> "0x0000000000000000000010066E18" ) ,
3=> array( 0=> "0xE2004465650F015224401B255262" ),
);


$arrayiter = new RecursiveArrayIterator($array_1);
$iteriter = new RecursiveIteratorIterator($arrayiter);
foreach ($iteriter as $value) {
    $array_1[] = $value;
}
$arrayiter = new RecursiveArrayIterator($array_2);
$iteriter = new RecursiveIteratorIterator($arrayiter);
foreach ($iteriter as $value) {
$array_2[] = $value;
}

$results = array_diff($array_1, $array_2); 

echo'<pre>';
var_dump($results);
echo'</pre>';