开发者

PHP Return Section of a Multidimentional Array

I have an array with a series of event IDs organized like this: $event['year']['month']['day'] = $event_id (full structure below).

Basically, I want to output the next 5 events (or so) from a given date (such as today).

I've gone through the PHP manual, but haven't found a suitable solution. I suppose I could just iterate through each step, but there could be hundreds of events. If I knew the offset, I could use array_slice, but I'm not sure how to get the offset without looping through the entire array. If I could set the pointer, then I would just iterate through. But I gather there isn't a way to set a pointer in a PHP array.

A specific MySQL query isn't very feasible either since the data isn't well organized (this is using meta keys in a Wordpress database). I'd probably have to use a number of JOINs, so I think the performance h开发者_开发问答it would be bad.

Given the current year, month, and day (e.g., $event[$year][$month][$day], I want to just show the next 5 events.

The structure looks like this:

Array
(
    [2010] => Array
        (
            [1] => Array
                (
                    [1] => Array
                        (
                            [594] => "Event"
                        )
                )
            [2] => Array
                (
                    [1] => Array
                        (
                            [592] => "Event",
                            [524] => "Event"
                        )

                    [2] => Array
                        (
                            [580] => "Event"
                        )
    [2011] => Array
        (
            [1] => Array
                (
                    [1] => Array
                        (
                            [587] => "Event"
                        )
                )
        )
)

Thoughts? Sorry if this description is a bit complicated. Thanks!

Edit: Typos


This won't be fast because it iterates through the entire array. In fact, this is probably a bad way to do it. Unfortunately, this might be the most straightforward way. The actual date of each event isn't stored directly, but we can derive it.

$earliest_date = strtotime('next Monday'); // or whatever.
$new_events = array();
foreach($array as $year => $a) {
    foreach($array[$year] as $month => $b {
        foreach($array[$year][$month] as $day => $events) {
            $date = strtotime("$year-$month-$day");
            if($date >= $earliest_date)
                $new_events[] = array( 'date' => $date, 'events' => $events );
            if(count($new_events) >= 5)
                break 3; // Breaks out of all three loops.
        }
    }
}

Of note, the new array picks the five events on or after the date, but because there can be multiple events per day (per your sample data), it's possible that only some of the events may be in the array.


If the array is ordered, you could use binary search to find the date of today or the closest thing before today (if there are no entries for today).

Then, you would have to iterate through the array as if it were flat and output the next three entries.

This is better than iterating through the whole array since it doesn't take linear time.


Some optimization for this solution:

$earliest_date  = strtotime('next Monday'); // or whatever.

$earliest_year  = date('Y', $earliest_date); 

$earliest_month = (int)date('m', $earliest_date); //(int) for leading zero remove

$earliest_day   = date('j', $earliest_date); $new_events = array();

foreach($array as $year => $a) {
    if ($year>=$earliest_year) {
        foreach($array[$year] as $month => $b {
            if ($month>=$earliest_month) {
                foreach($array[$year][$month] as $day => $events) {
                    $date = strtotime("$year-$month-$day");
                    if($date >= $earliest_date) {
                        $new_events[] = array( 'date' => $date, 'events' => $events );
                        if(count($new_events) >= 5) {
                            break 3; // Breaks out of all three loops.
                        }
                    }
                }
            }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜