<?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)));
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)