【问题标题】:Check nested Array values, if a match: combine data from both检查嵌套数组值,如果匹配:合并两者的数据
【发布时间】:2020-02-12 18:17:48
【问题描述】:

我遇到了一个奇怪的情况,我不确定我的解决方案是否会奏效,或者是否可行……它适用于 Wordpress 网站,但我希望它是一个通用的循环查询,而不是某些东西特定于 WP... 但让我们尝试一下:

我有一个数组,类似这样(在查找 WP 事件帖子时会发生变化):

$events = Array
(
[0] => WP_Post Object
    (
        [ID] => 8717
        [post_author] => 5
        [post_date] => 2020-01-28 14:25:36
        [post_parent] => 2766
        [post_title] => Film 1
    )
[1] => WP_Post Object
    (
        [ID] => 8716
        [post_author] => 5
        [post_date] => 2020-01-28 14:25:36
        [post_parent] => 2769
        [post_title] => Film 2
    )
[2] => WP_Post Object
    (
        [ID] => 8716
        [post_author] => 5
        [post_date] => 2020-01-28 14:25:39
        [post_parent] => 2766
        [post_title] => Film 1
    )
)

我用 foreach 循环遍历数组:

foreach ( $events as $event ) { global $post; 
    echo tribe_event_featured_image( $event->ID, 'medium' );
    // This outputs an image, pulled from the event post

    echo '<h4>'. $event->post_title . '</h4>';

    tribe_get_start_date($event, false, $format= 'h:i')
    //This outputs a start time, pulled from the event
}

但是,我需要先检查 post_parent 是否相同。如果是这样,我需要将它们一起显示,而不是单独显示这些项目。

所以目前的输出是:

IMAGE
Film 1
10:00

IMAGE
Film 2
12:00

IMAGE 
Film 1
14:00

但是,电影 1 和电影 3 具有相同的“post_parent”键值,所以我希望输出为:

IMAGE
Film 1
10:00, 14:00  (Note 14:00 is the time pulled from 3rd object in array)

IMAGE
Film 2
12:00

【问题讨论】:

  • 这里有类似的问题和答案。 stackoverflow.com/questions/60192457/…
  • 谢谢。我已经看到并尝试实施,但它似乎对我不起作用。您能否使用我的特定代码提供答案?

标签: php arrays wordpress iteration


【解决方案1】:

为了达到您想要的结果,您需要通过parent_post过滤事件时间并跳过已经显示的事件或检查post_parent以确保它不是null0,这可以通过检查empty($event-&gt;post_parent) 来实现。

另外,您只需要声明一次global,因为foreach 不会分隔变量范围。

示例:https://3v4l.org/pYjQJ

global $post;
$matchedParents = [];
foreach ($events as $event) { 
    if (in_array($event->ID, $matchedParents)) {
        /* post_parent has already been output, skip the event */
        continue;
    }

    echo tribe_event_featured_image($event->ID, 'medium');
    echo '<h4>'. $event->post_title . '</h4>';

    /* display the times based on post_parent */
    $eventTimes = [];
    if (empty($event->post_parent)) {
       /* ensure post_parent is not empty */
       $eventTimes[] = tribe_get_start_date($event, false, $format = 'h:i');
     } else {
        foreach ($events as $subEvent) {
           if ($event->post_parent === $subEvent->post_parent) {
            /* store the matched events */
               $matchedParents[] = $subEvent->ID;
            /* store the event times to be output later */
               $eventTimes[] = tribe_get_start_date($subEvent, false, $format = 'h:i');
           }
       }
    }
    /* optionally sort the times */
    natsort($eventTimes);

    /* output the event times as a CSV */
    echo implode(', ', $eventTimes);
}

结果

IMAGE
<h4>Film 1</h4>
10:00, 14:00

IMAGE
<h4>Film 2</h4>
14:25

IMAGE
<h4>Film 4</h4>
14:25

IMAGE
<h4>Film 5</h4>
14:00

【讨论】:

  • 这和上面的问题一样:如果post_parent = 0,那么它将与前一个时间结合起来,不显示单个事件
  • @user2115227 更新了检查null0 post_parent 的条件。价值观
  • 这实际上是比上面更好的答案。谢谢,效果很好!
  • @user2115227 修复了复制/粘贴中的拼写错误,empty post_parent 应该使用 $event 而不是 $subEvent
【解决方案2】:

检查这个答案。我没有测试它,但希望它有效。

    $filtered_data = []; //An temp array
    // loop your array to create a new array with parent id as key
    foreach ( $events as $data ){
        $filtered_data[$data->post_parent][] = $data;
    }

    // loop the new array to display content as you desired
    foreach ( $filtered_data as $event_data ) { global $post; 
        echo tribe_event_featured_image( $event_data[0]->ID, 'medium' );
        // This outputs an image, pulled from the event post, since there can be multiple post ids, it takes first id to get the Image

        echo '<h4>'. $event_data[0]->post_title . '</h4>';  // same here, It will display first post title

// loop the sub array to get all the times
        foreach($event_data as $event) {
            echo  tribe_get_start_date($event, false, $format= 'h:i') . ',';
        }
        //This outputs a start time, pulled from the event
    }

【讨论】:

  • 谢谢,但它不起作用 - 什么都没有显示?
  • 修正了错字,这是一种享受 - 谢谢!!
  • 请记住,当前的方法还会导致在仅检索一次时显示尾随逗号,,如果打算订购时间可能会乱序跨度>
  • 如果post_parent = 0,则与前一个时间合并,不显示单个事件
  • 要完成对空post_parent 的检查,您可以使用$filtered_data[$data-&gt;post_parent ?: uniqid('zero_')][] = $data;,这将为占位符使用一个唯一的ID,该占位符不应与任何其他post_parent id 匹配,从而强制进行额外的迭代发生。请参阅:3v4l.org/oiIjP 进行演示。
猜你喜欢
  • 1970-01-01
  • 2016-08-02
  • 2016-07-08
  • 1970-01-01
  • 1970-01-01
  • 2019-08-13
  • 1970-01-01
  • 2018-01-16
  • 1970-01-01
相关资源
最近更新 更多