TheBrandon Posted June 1, 2012 Share Posted June 1, 2012 Hello all, I have 2 arrays containing numbers; I just need to extract the unique ones from the first array. For example, right now I have this: Quote existing_Array: Array ( [31] => 31 [34] => 34 [41] => 41 [43] => 43 [44] => 44 [62] => 62 [63] => 63 [64] => 64 [71] => 71 [97] => 97 [233] => 233 [234] => 234 [291] => 291 ) submitted_Array: Array ( [31] => 3 [34] => 2 [41] => 3 [43] => 1 [44] => 2 [63] => 1 [64] => 2 [71] => 6 [97] => 5 [233] => 3 [234] => 1 [291] => 2 ) I want this: Quote new_Array: Array ( [62] => 62 ) I thought array_diff did this exactly, but when I do: $new_Array = array_diff($existing_Array, $submitted_Array); It's giving me this: Quote Array ( [31] => 31 [34] => 34 [41] => 41 [43] => 43 [44] => 44 [62] => 62 [63] => 63 [64] => 64 [71] => 71 [97] => 97 [233] => 233 [234] => 234 [291] => 291 ) Can someone help me get this to work? Link to comment https://forums.phpfreaks.com/topic/263488-help-getting-the-differences-between-2-arrays/ Share on other sites More sharing options...
TheBrandon Posted June 1, 2012 Author Share Posted June 1, 2012 I got it. I put it inside of a loop using array_key_exists. If anyone has a better method, I'd love to know! foreach($existing_Array as $key => $value){ if(!array_key_exists($key, $submitted_Array)){ $new_Array[] = $key; } } Link to comment https://forums.phpfreaks.com/topic/263488-help-getting-the-differences-between-2-arrays/#findComment-1350341 Share on other sites More sharing options...
Barand Posted June 1, 2012 Share Posted June 1, 2012 try $diff = array_diff_key ($existing_array, $submitted_array); Link to comment https://forums.phpfreaks.com/topic/263488-help-getting-the-differences-between-2-arrays/#findComment-1350358 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.