Jump to content

Compare associative array to a normal array


Vitamin

Recommended Posts

I have a associative array and I need to compare it to a normal array.

 

associative will have anywhere from 2-8 of these 
['name']
['color']
['uid']

 

normal array will have anywhere from 1-8 names
['name']

 

What I need to do is get the name of the people that are in the associative array that are not in the normal array.

 

I've tried all sorts of different loops and what not and I cant seam to get anything to work in every case.

Thanks for the hint to array_intersect() never knew it existed.

 

Still not exactly what I'm looking for because it returns the values that are in both. While I want the values that are in one, but not the other.

 

Though I was looking at the doc page for array_intersect() and I think there is a comment in there that will do what I'm looking for.  I'll be back if I can't get something figured out.

Ah, sorry, I misread the question. There's also array_diff().

 

Yea that is what I was working with last night.  Just read the docs page on it again though (was reading it last night as well).

 

This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1[0], $array2[0]);.

 

That is where I was going wrong because one of the arrays was 2-dimensional I was not getting the right results.  Got it solved now thanks!

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.