Skip to content
Advertisement

Search an array for a key and output the value

I have a large array in PHP that has this snippet of data. Is there a way to search through an array and find the value of “date” without traversing through all the nodes?

It should be noted that “date” in itself isn’t unique but $subscription->schedule_next_payment->date is.

[schedule_next_payment] => WC_DateTime Object

(
    [utc_offset:protected] => 0
    [date] => 2021-01-29 14:26:43.000000
    [timezone_type] => 3
    [timezone] => Europe/London
)

Advertisement

Answer

If the array is sorted by date, you could perform a binary search.

If not, I’m afraid you need to either change the way you get the data (like changing database systems) or somehow handle having to loop through all that.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement