I'm trying to get the bottom values of an array with two conditions as below :
<?php $data=[1,2,3,'web',4,5,6,'web',7,8,9]; ?>
The output will be 7 8 9.
<?php $data= [1,2,3,'web',4,5,6,'web',7,8,9,'web','web']; ?>
<?php $data= [1,2,3,'web',4,5,6,'web',7,8,9,'web']; ?>
Both of conditions will have the same output: 7 8 9.
Logic: Reverse the array and check weather the given element is integer, if it is an integer, put it into a temporary array, otherwise, check if the process of saving integer data has already been started, if yes, break the loop otherwise, continue with the loop. Finally, reverse the array again to get the data in same format.
<?php
$data= [1,2,3,'web',4,5,6,'web',7,8,9,'web'];
$process = false;
foreach(array_reverse($data) as $d){
if(is_int($d)){
$process = true;
$temp[] = $d;
}
else if($process) break;
}
$result = array_reverse($temp);
print_r($result);
?>