3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * An array in DESCENDING order (OP) */ $array = [ 61029, 64698, 67355, 70000, // has bubble 43651, 48346, 52029, // has bubble 48029, 48698, 49355, 50000, ]; /** * An array in ASCENDING order */ $asc_array = [ 10, 20, 30, 40, 50, 45, //Has bubble 55 ]; /** * Given an array, identify a "bubble", aka. an increasing value in an otherwise decreasing value array. * Returns the $key where the bubble resides. */ function identifyBubble($array){ foreach($array as $id => $item){ if(!$id){ continue; } if(!$array[$id+1]){ continue; } if(($array[$id-1] < $array[$id]) && ($array[$id] > $array[$id+1])){ return $id; } } return false; } /** * If an array is in ASCENDING order, switch it around, * otherwise return the array as is. */ function makeArrayDescending($array){ if(reset($array) < end($array)){ return array_values(array_reverse($array)); } return $array; } var_dump(identifyBubble($array)); var_dump(makeArrayDescending($asc_array)); var_dump(identifyBubble(makeArrayDescending($asc_array)));

preferences:
43.69 ms | 402 KiB | 5 Q