I have an array() of years, they are in order lowest to highest, they are not sequential, so 1100, 1295, 1733,1734, 1980 etc. The key starts at 0 up to the number of years. So [0] => 1100, [1] => 1295.. [5] => 1980. What i am trying to do is pull a range out from the middle, so a user inputs start year = 1200 and end year = 1900. Can anyone help me create a new array from my original array with values that are between the start and end years. Thanks for your help. I am using PHP version 5.6+ cant remember the exact version.
1 Answer
I think array_filter with a custom callback is the shortest way to get the time span:
$array = [1100, 1295, 1733, 1734, 1980];
$start = 1200;
$end = 1733;
$result = array_filter($array, function ($value) use ($start, $end)
{return $start <= $value && $value <= $end;});
var_dump($result);
But there are many other ways.
1 Comment
chris davies
Yes, I am checking it out on my server, cheers ;) Building a timeline of historic caving events.. Bring on the darkness!
array_search()function to identify your start and end keys, and then atarray_slice()to extract the range if the user inputs exact values from your array; or evenarray_filter()to extract the values between start and end dates