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? Link to comment https://forums.phpfreaks.com/topic/217486-pick-highest-number/ 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 Link to comment https://forums.phpfreaks.com/topic/217486-pick-highest-number/#findComment-1129135 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! Link to comment https://forums.phpfreaks.com/topic/217486-pick-highest-number/#findComment-1129137 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. Link to comment https://forums.phpfreaks.com/topic/217486-pick-highest-number/#findComment-1129138 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)); Link to comment https://forums.phpfreaks.com/topic/217486-pick-highest-number/#findComment-1129141 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. Link to comment https://forums.phpfreaks.com/topic/217486-pick-highest-number/#findComment-1129151 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.