0

I am using the following code to find if $devices_ForUser doesn't have any values listed in $device_CurrentIDS.

$results = array_diff($devices_ForUser, $device_CurrentIDS);

When I check if $results is empty, it shows as empty when it should not be empty.

$device_CurrentIDS = [
    ["0x0000000000000013620010713E5A"],
    ["0x000000000000001077002942CE57"],
    ["0x20120427850602000103026906B2"],
    ["0xE20062969619018824701915D683"],
    ["0x0000000008002216572D06103CF6"],
    ["0xE2004465650F015224401B255262"],  // found in the other array
    ["0xE20092012053100000002850D14E"],
    ["0x300833B2DDD901400000000039BB"],
    ["0xE2002996961802570960B3F06912"],
    ["0x000000000000000000501420B8B6"],
    ["0x201203298478040001020252A5EC"],
    ["0xE20010008007026819204FCAF906"],
    ["0x0000000000000000000007485DF6"],
    ["0xE2001036990F008812908EA5481C"],
    ["0x0019000000000000000043B94C3A"],  // found in the other array
    ["0x0000000000000000004004490529"],  // found in the other array
    ["0x0000000000000000000010066E18"],  // found in the other array
    ["Error 0100: syntax error at ''"],
    ["Error 0102: Error performing query"],
    [null]
];

and

$devices_ForUser = [
    ["0x0019000000000000000043B94C3A"],
    ["0x0000000000000000004004490529"],
    ["0x0000000000000000000010066E18"],
    ["0xE2004465650F015224401B255262"]
];
3
  • $devices_ForUser, $device_CurrentIDS - which of those is the first? Commented Oct 15, 2013 at 0:47
  • Check my question it is at the bottom. Commented Oct 15, 2013 at 0:49
  • This question is missing its exact desired result. Even if you correctly replace array_diff() with array_udiff() (which is built to handle multidimensional data), your result will still be empty according to the logic that you have asked for. 3v4l.org/nYHdl Commented Nov 18, 2024 at 7:16

2 Answers 2

1

see http://uk.php.net/manual/en/function.array-diff.php :

This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1[0], $array2[0]);.

You have a nested array, this cant work.

EDIT:

This may work:

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);
Sign up to request clarification or add additional context in comments.

6 Comments

Oh I see, so I just have to iterate through?
no, this will be not enough.. you will have to get all the values in one level of an array, (for both arrays) then you can use the array_diff.
Could you supply me with an example?
Awesome this worked perfect, who ever thought using recursive could be useful!
actually the results seems to be storing correctly but there is a gap between 3 and 5 within the array index, why is that?
|
1

You can use SPL to do what you want.

$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>';

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.