Page 1 of 1

Array_diff

Posted: Wed Aug 10, 2005 3:18 pm
by robjime
I was using array_diff to find the difference between two arrays which contained a list of files from two directories. The problem is that array_diff returns the difference but keeps the keys the same. Howd get that information from the array if the first key isn't 0, or if theres a bunch of random keys.

Posted: Wed Aug 10, 2005 4:08 pm
by feyd
what?

Posted: Wed Aug 10, 2005 4:28 pm
by robjime
my array diff array returned [2] filename.jpg, now howd i pull that information out of that array without knowing its key?

Posted: Wed Aug 10, 2005 4:35 pm
by s.dot
it's returning the 3rd value from the array

$array[0], $array[1], $array[2]

Posted: Wed Aug 10, 2005 9:34 pm
by robjime
but theres only one value in that array, $thediff, and its key is two, but it will always be different, look at array_diff, so howd pull information from an array that has random key values?

array{
img1.jpg[2]
imge3.jpg[6]
}

Posted: Wed Aug 10, 2005 9:37 pm
by feyd
you're sending it the arrays.. it finds that information right there..
array_diff() returns an array containing all the values of array1 that are not present in any of the other arguments. Note that keys are preserved.
emphasis added.

Posted: Wed Aug 10, 2005 9:41 pm
by robjime
I KNOW, i want to pull that data out of the array,
so howd pull information from an array that has random key values

Posted: Wed Aug 10, 2005 10:23 pm
by feyd
so... you want to remove all differences in the arrays?

Posted: Wed Aug 10, 2005 10:39 pm
by nielsene
you can use either

Code: Select all

$keys=array_keys($arr); // get an array starting at 0, of the keys
for ($i=0;$i<count($keys);$i++) {
  // do stuff with $arr[$keys[$i]]
}
// or probably much better
foreach ($arr as $key=>$value) {
 // loops over all key=>value pairs, doesn't care about order/non-zeroness
}