Vitamin Posted November 12, 2010 Share Posted November 12, 2010 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 https://forums.phpfreaks.com/topic/218523-compare-associative-array-to-a-normal-array/ Share on other sites More sharing options...
BlueSkyIS Posted November 12, 2010 Share Posted November 12, 2010 post some non-working code so we can see what's wrong? Link to comment https://forums.phpfreaks.com/topic/218523-compare-associative-array-to-a-normal-array/#findComment-1133641 Share on other sites More sharing options...
Pikachu2000 Posted November 12, 2010 Share Posted November 12, 2010 Sounds like a job for array_intersect(). Link to comment https://forums.phpfreaks.com/topic/218523-compare-associative-array-to-a-normal-array/#findComment-1133645 Share on other sites More sharing options...
Vitamin Posted November 12, 2010 Author Share Posted November 12, 2010 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 https://forums.phpfreaks.com/topic/218523-compare-associative-array-to-a-normal-array/#findComment-1133655 Share on other sites More sharing options...
Pikachu2000 Posted November 12, 2010 Share Posted November 12, 2010 Ah, sorry, I misread the question. There's also array_diff(). Link to comment https://forums.phpfreaks.com/topic/218523-compare-associative-array-to-a-normal-array/#findComment-1133658 Share on other sites More sharing options...
Vitamin Posted November 12, 2010 Author Share Posted November 12, 2010 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 https://forums.phpfreaks.com/topic/218523-compare-associative-array-to-a-normal-array/#findComment-1133662 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.