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)));
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.4, 8.3.6 - 8.3.7
int(3) array(7) { [0]=> int(55) [1]=> int(45) [2]=> int(50) [3]=> int(40) [4]=> int(30) [5]=> int(20) [6]=> int(10) } int(2)
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 int(3) array(7) { [0]=> int(55) [1]=> int(45) [2]=> int(50) [3]=> int(40) [4]=> int(30) [5]=> int(20) [6]=> int(10) } int(2)

preferences:
134.98 ms | 402 KiB | 213 Q