0

I like to compare an array from a database against another array to generate a new array of missing ids using isset or array_key_exists. Here is the array from my database I am creating.

foreach ($listings as $listing) {
    $local_ids[$listing['id']] = 'true';
}

$local_ids = array(
    '567' => true,
    '568' => true,
    '569' => true,
    '570' => true,
    '571' => true,
    '572' => true,
    '573' => true
);

$new_ids = array(
    '568',
    '569',
    '572',
    '573',
    '574',
    '575',
    '576',
    '577'
);

Taking the above two arrays, I would like to cycle through them to give me a result that 567, 570, 571 have been removed.

I altered the $new_ids as this list may or may not contain ids that are new and not in $local_ids. These can be ignored, I just need the ones removed.

Tim
  • 403
  • 1
  • 6
  • 20
  • array_diff() would do that. – Ja͢ck May 19 '13 at 15:00
  • [`array_diff(array_keys($local_ids), $new_ids)`](http://us.php.net/array_diff) – DCoder May 19 '13 at 15:00
  • I will try that, would that work for 30,000+ results to compare? – Tim May 19 '13 at 15:01
  • That didn't take long to run, it did though return all keys that exist and not the ones missing. – Tim May 19 '13 at 15:07
  • I tried both of these, and they give the results on a smaller scale. On a large scale it works, the results are not correct. – Tim May 19 '13 at 15:16
  • I ran this and checked both to make sure, it basically giving ids that should not be deleted. It is because of such a large arrays? – Tim May 19 '13 at 15:35
  • I altered the `$new_ids` array, could new ids alter what I am doing? – Tim May 19 '13 at 16:12
  • Jack and DCoder, yes this is working. Just looks like it isn't as there are a very large list of ids in `$new_ids`; isn't there way getting just the ones removed from the first list? I need the results in the end to be `567, 570, 571` and not `567, 570, 571, 574, 575, 576, 577`. I believe this is where `isset` or `array_key_exists` search would be more efficient. – Tim May 19 '13 at 16:51

1 Answers1

0

i know a method which goes like this

<?php
$a1 = array("a" => "one", "two", "three", "four");
$a2 = array("b" => "one", "two", "three");
$result = array_diff($array1, $array2);

print_r($result);
?>
or

<?php
$a1 = array("a" => "one", "b" => "two", "c" => "three", "four");
$a2 = array("a" => "one", "two", "three");
$result = array_diff_assoc($array1, $array2);
print_r($result);
?>
argentum47
  • 2,385
  • 1
  • 18
  • 20