I'm trying to get the bottom values of an array with two conditions as below :
- Get all numeric values from the bottom until the next value is a string then stop the instruction, and leave all other values even if they are numeric
- If the first values from the bottom are strings skip them and execute the first condition.
<?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.
<?php $data = [1,2,3,'web',4,5,6,'web',7,8,9]; $newArray = array(); // resultant array foreach ($data as $key => $value) { if(is_int($value)){ $newArray[] = $value; } else{ $newArray = array(); // reset if not integer } } echo "<pre>"; print_r($newArray); ?>