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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.