EchoFool Posted November 1, 2010 Share Posted November 1, 2010 Hey, I have an array which stores a bunch of numbers lets say: [0] = 1[1] = 6 Now a second array has: [0] = 5 [1] = 6 What im trying to do - is find the first highest number from the second array that is not found in the first array.Both arrays are sorted already by the way using sort(). What is the best way to do this? Quote Link to comment Share on other sites More sharing options...
Andy-H Posted November 1, 2010 Share Posted November 1, 2010 $array = array( 1, 6 ); $array2 = array( 5, 6 ); $diff = array_diff($array2, $array); sort($diff, SORT_NUMERIC); echo 'Highest unique array value in $array2: ' . $diff[count($diff) - 1]; array_diff Quote Link to comment Share on other sites More sharing options...
EchoFool Posted November 1, 2010 Author Share Posted November 1, 2010 Thanks will play around with it and see what i get! Quote Link to comment Share on other sites More sharing options...
Maq Posted November 1, 2010 Share Posted November 1, 2010 $array = array( 1, 6 ); $array2 = array( 5, 6 ); $diff = array_diff($array2, $array); sort($diff, SORT_NUMERIC); echo 'Highest unique array value in $array2: ' . $diff[count($diff) - 1]; array_diff Was going to reply pretty much the same but you can also use max rather than sorting. Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 1, 2010 Share Posted November 1, 2010 Was going to reply pretty much the same but you can also use max rather than sorting. Exactly. You can reduce the code down to a single line as follows: $array1 = array( 1, 6 ); $array2 = array( 5, 6 ); echo 'Highest unique array value in $array2: ' . max(array_diff($array2, $array1)); Quote Link to comment Share on other sites More sharing options...
Andy-H Posted November 1, 2010 Share Posted November 1, 2010 lol fair play, haven't used max in a long time. Forgot it existed. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.