PHP 返回多维数组的部分

发布于 2024-09-11 11:52:38 字数 1381 浏览 3 评论 0原文

我有一个包含一系列事件 ID 的数组,如下所示: $event['year']['month']['day'] = $event_id (完整结构如下)。

基本上,我想输出给定日期(例如今天)的接下来 5 个事件(左右)。

我已经阅读了 PHP 手册,但没有找到合适的解决方案。我想我可以迭代每个步骤,但可能有数百个事件。如果我知道偏移量,我可以使用 array_slice,但我不确定如何在不循环整个数组的情况下获取偏移量。如果我可以设置指针,那么我就会迭代。但我认为没有办法在 PHP 数组中设置指针。

特定的 MySQL 查询也不太可行,因为数据组织得不好(这是在 Wordpress 数据库中使用元键)。我可能必须使用多个 JOIN,因此我认为性能会受到严重影响。

给定当前的年、月、日(例如,$event[$year][$month][$day],我只想显示接下来的 5 个事件。

结构如下所示:

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"
                        )
                )
        )
)

想法?抱歉,如果此描述是有点复杂。

编辑:错别字

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 hit 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

变身佩奇 2024-09-18 11:52:38

这不会很快,因为它会遍历整个数组。事实上,这可能是一个糟糕的方法。不幸的是,这可能是最直接的方法。每个事件的实际日期并不直接存储,但我们可以导出它。

$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.
        }
    }
}

值得注意的是,新数组选择该日期或之后的五个事件,但由于每天可能有多个事件(根据您的示例数据),因此数组中可能只有部分事件。

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.

怪我入戏太深 2024-09-18 11:52:38

如果数组是有序的,您可以使用二分搜索来查找今天或最近的日期今天之前的事情(如果今天没有条目)。

然后,您必须像平数组一样迭代该数组并输出接下来的三个条目。

这比迭代整个数组更好,因为它不需要线性时间。

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.

梦毁影碎の 2024-09-18 11:52:38

该解决方案的一些优化:

$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.
                        }
                    }
                }
            }
        }
    }
}

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.
                        }
                    }
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文