Array_diff

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
robjime
Forum Commoner
Posts: 46
Joined: Sat Apr 03, 2004 12:26 pm
Location: the RO

Array_diff

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what?
robjime
Forum Commoner
Posts: 46
Joined: Sat Apr 03, 2004 12:26 pm
Location: the RO

Post by robjime »

my array diff array returned [2] filename.jpg, now howd i pull that information out of that array without knowing its key?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

it's returning the 3rd value from the array

$array[0], $array[1], $array[2]
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
robjime
Forum Commoner
Posts: 46
Joined: Sat Apr 03, 2004 12:26 pm
Location: the RO

Post 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]
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
robjime
Forum Commoner
Posts: 46
Joined: Sat Apr 03, 2004 12:26 pm
Location: the RO

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

so... you want to remove all differences in the arrays?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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
}
Post Reply