2

Here is array 1:

Array ( [ABC01] => 10.123.456.78
        [ABC02] => 10.123.456.79
        [ABC03] => 10.123.456.80
        [ZYX99] => 10.123.456.81
      )

Here is array 2:

Array ( [0] => ABC01
        [1] => ABC02
        [2] => ABC03
      )

I'm trying to find the difference between these two arrays and return the following (as you can see, the host name and then the corresponding ip address of an item not found in array 2):

Array ( [ZYX99] => 10.123.456.81)

I've been looking through the different PHP array functions and am overwhelmed by the amount of them: http://www.w3schools.com/php/php_ref_array.asp

l33tspeak
  • 95
  • 3
  • 13

1 Answers1

6

This should work for you:

(Here I just used array_diff_key() to get the difference of the keys. The second array I flipped with array_flip() so to change the values to keys)

<?php

    $arr1 = array(
            "ABC01" => "10.123.456.78",
            "ABC02" => "10.123.456.79",
            "ABC03" => "10.123.456.80",
            "ZYX99" => "10.123.456.81"
    );

    $arr2 = array("ABC01", "ABC02", "ABC03");

    $result = array_diff_key ($arr1, array_flip($arr2));
    print_r($result);

?>

Output:

Array ( [ZYX99] => 10.123.456.81 )
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • Thanks for this! Unfortunately, this is not outputting the host name,just the ip (Array ([0] => 10.123.456.78). I'll look into it further. – l33tspeak Feb 26 '15 at 17:33