4

I have an array with indexes as timestamps. I tried array_slice to get all values between a time range but it doesn't seem to work.

$data = array_slice(["1549440811" => 1, "1549448226" => 2, "1551108588" => 3 ], 0, 1549460338);

I should get the $data as ["1549440811" => 1, "1549448226" => 2] but it doesn't work that way.

To get the right data I have to use

$data = array_slice(["1549440811" => 1, "1549448226" => 2, "1551108588" => 3 ], 0, 2);

But the problem is the records can have random timestamps and no. of records. So I am unable to figure out the offset which is 2 in this case.

I know the code below with a few changes might work for small range but not for my timestamps as $myrange would have a lot of data.

$myrange = range(0,1549460338);
$output = array_intersect(["1549440811" => 1, "1549448226" => 2, "1551108588" => 3 ] , $myrange );

I am avoiding looping through the array as the array has a lot of data. Also I have a lot of timestamps to check. This code is a simplified logic of a bigger code with records from database indexed with timestamps.

Is there any other way I could get the desired data?

5
  • why you filtering array, why not filter in mysql query or whatever use you database?? Commented Apr 3, 2019 at 8:36
  • @nageennayak i have a loop on data I get from database and the timestamp 1549460338 is of a record in the loop, I avoided querying the database in the loop to fetch the other data. I got the other data in one query indexed by timestamp instead Commented Apr 3, 2019 at 8:39
  • @nageennayak the other data that I am fetching is being reused by other records in the loop as well Commented Apr 3, 2019 at 8:41
  • is the array already sorted by timestamp? Commented Apr 3, 2019 at 9:19
  • @acd yes sorted Commented Apr 3, 2019 at 9:56

1 Answer 1

2

Simple for-loop should do:

$arr = ["1549440811" => 1, "1549448226" => 2, "1551108588" => 3 ];
$range = "1549460338";
foreach($arr as $k => $v) {
    if ($range > $k)
        break;
    $newArr[$k] = $v;
}

You can also use array_filter (doc):

$filtered = array_filter( $arr,
    function ($key) use ($range) {return $range > $key;},
    ARRAY_FILTER_USE_KEY
);

Example: 3v4l

Edit:

Fastest way (consider your array is sorted) is to extract the keys with $keys = array_keys($arr); and then search for the $range using binary search (O(log(n))) -> then use array_slice with that index.

Sign up to request clarification or add additional context in comments.

7 Comments

I prefer not to loop as the data is too much.
@MehravishTemkar update the post using array_filter - is it better?
thanks. let me implement it in my code and get back to you
@MehravishTemkar on the backend, it would be loop
thanks. thousands of records in the array as well as for timestamps, avoiding things that would slow it down. @DanyalSandeelo
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.