Count from certain position in an array

I have an array thats called $mainArray

and it contains hundreds of hotels and 1 of the fields is a datetime field called

TourCheckPublished

To get pagination to work correctly the next 12 hotels show in order of date, but I also need to count from that new point to the end of the array each time, and the last date is stored in a variable called $keyID

So how can I do something like this below, so I count the number of hotels left in the array after the last date value that exists in $keyID

So something like this, but this is code speak

$num_rows = count($hotelArray); where $hotel['TourCheckPublished'] < $keyID

Once you know which hotel to start at, can’t you subtract that array position from the result of count()?

Also if this is related to the other thing, did you look at using usort() instead of uasort() to sort the array by date, which would leave all the first-dimension keys (which were numeric) in numeric order, so you could continue to use the id to paginate and maybe help with this issue?

if i get this right, array_slice() may also help.

Hi droopsnoot, ye it is related and you’ll have to forgive me as yes you did say about usort and yes I did change and it completely went out of my mind that I can now organise by key rather than the date.

I’ve gone ahead as I usually do and organised all the search criteria to work with the date stamp now, andyes you are right to use the array position from the result, but by me still working with the datetime, I didnt see it sorry.

If you stick to using dates for the comparison, you could use array_find() to find the date/time marker position you’re looking for, assuming they will be unique or close enough. I’m not sure how you’d then count to the end without using something like a loop, I’m sure there must be a way. But going back to the other sort method would make it easy as you could just use the first-dimension key in the calculation since it’s in sequence, even if you keep the date/time handling and searching that you’ve done now.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.