Determining the maximum using max
only properly works for integers, so I would use array_map
with hexdec
to achieve that.
Then all it needs is a comparison of the value at position 1 with that maximum, and if they are the same, then the index gets added to the result array:
$result = [];
foreach($hex_splited as $key => $value) {
if(hexdec($value[1]) == max(array_map('hexdec', $value))) {
$result[] = $key;
}
}
var_dump($result);
This gets you 0, 1, 3
as result. (As IMHO it should be, according to your stated requirements - the value 00
at position 1 in the first sub-array is also the maximum of all values in that sub-array. If you need to exclude 0 values, then you’ll have to modify accordingly.)
EDIT:
if all elements are equal, then return nothing
Okay, that can also be implemented quite easily - we use array_count_values
, and if the count of that is only 1, then that means all the elements had the exact same value.
So then it becomes
foreach($hex_splited as $key => $value) {
if(count(array_count_values($value)) != 1 &&
hexdec($value[1]) == max(array_map('hexdec', $value))) {
$result[] = $key;
}
}
With that, you get [1, 3]
as $result here.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…