Jump to content

Pick highest number


EchoFool

Recommended Posts

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


$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

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

Archived

This topic is now archived and is closed to further replies.

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