Rob
Rob

Reputation: 6380

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
)

Upvotes: 0

Views: 72

Answers (1)

Ilhan Ates
Ilhan Ates

Reputation: 26

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.

Upvotes: 1

Related Questions