SystemX17
SystemX17

Reputation: 3715

How to compare 2 arrays for common items in PHP?

I am writing a script to compare the achievements of one player to another in a game. In each of the arrays the id and timestamp will match up on some entries. I have included a sample of one of the start of the 2 separate arrays:

Array
(
    [0] => Array
        (
            [id] => 8213
            [timestamp] => 1384420404000
            [url] => http://www.wowhead.com/achievement=8213&who=Azramon&when=1384420404000
            [name] => Friends In Places Higher Yet
        )

    [1] => Array
        (
            [id] => 6460
            [timestamp] => 1384156380000
            [url] => http://www.wowhead.com/achievement=6460&who=Azramon&when=1384156380000
            [name] => Hydrophobia
        )

I want to find all of the array items where the id and timestamp match. I have looked into array_intersect but I don't think this is what I am looking for as it will only find items when the entries are identical. Any help much appreciated.

Upvotes: 2

Views: 634

Answers (3)

Rob Baillie
Rob Baillie

Reputation: 3470

If you want to go on an exploration of array callback functions, take a look at array_uintersect. It's like array_intersect except you specify a function to use for the comparison. This means you can write your own.

Unfortunately, you need to implement a function that returns -1, 0 or 1 based on less than, same as, greater than, so you need more code. But I suspect that it'll be the most efficient way of doing what you're looking for.

function compareArrays( $compareArray1, $compareArray2 ) {
    if ( $compareArray1['id'] == $compareArray2['id'] && $compareArray1['timestamp'] == $compareArray2['timestamp'] ) {
        return 0;
    }
    if ( $compareArray1['id'] < $compareArray2['id']  ) {
        return -1;
    }
    if ( $compareArray1['id'] > $compareArray2['id']  ) {
        return 1;
    }
    if ( $compareArray1['timestamp'] < $compareArray2['timestamp']  ) {
        return -1;
    }
    if ( $compareArray1['timestamp'] > $compareArray2['timestamp']  ) {
        return 1;
    }
}

var_dump( array_uintersect( $array1, $array2, "compareArrays") );

Upvotes: 0

Philip G
Philip G

Reputation: 4104

Try something like this:

<?php
   $key_match = Array();
   //Loop first array
   foreach($array as $key => $element){
      //Compare to second array 
      if($element == $array2[$key]){
          //Store matching keys
          $key_match[] = $key;
      }
   }
?>

$key_match will be an array with all matching keys.

(I'm at work and havn't had time to test the code)

Hope it helps

EDIT: Fully working example below:

<?php
$a1["t"] = "123";
$a1["b"] = "124";
$a1["3"] = "125";
$a2["t"] = "123";
$a2["b"] = "124";
$a2["3"] = "115";

$key_match = Array();

    //Loop first array
    foreach($a1 as $key => $element){
        //Compare to second array 
        if($element == $a2[$key]){
           //Store matching keys
           $key_match[] = $key;
   }
}

var_dump($key_match);
?>

Upvotes: 0

max
max

Reputation: 2817

You may use array_intersect_assoc function.

Upvotes: 1

Related Questions