首页 > 解决方案 > 弄清楚如何为过滤器循环合并数组

问题描述

我正在创建一个通过 Eventbrite API 显示事件的 Wordpress 网站。使用来自https://github.com/Automattic/eventbrite-api的 API

我能够得到数据。使用事件数据创建一个模板并创建一个滑块。我要做的是按场地 ID 和组织者 ID 过滤这些事件。

到目前为止我所做的。

创建了一个遍历事件并输入每个循环的循环。但它的出现是因为每个城市都是一个单独的数组,因此每个城市都有一个单独的下拉列表。

<div id="venues" class="form-group my-5">

    <form 
        id="venue-select" 
        class="venue-select input-group-prepend ml-auto mt-5" 
        action="<?php echo esc_url(home_url('/')); ?>" 
        method="get"
    >

        <?php printf(eventbrite_event_venue()); ?>

        <?php

        // Set up and call our Eventbrite query.
        $venue_filter = new Eventbrite_Query(apply_filters( 'eventbrite_query_args', array(
            'display_private' => true, // boolean)
            'status' => 'live',         // string (only available for display_private true)
            'limit' => 100,            // integer
        )));

        if ($venue_filter->have_posts()) :

            while ($venue_filter->have_posts()) : $venue_filter->the_post();

                $result =  array(eventbrite_event_venue());

                echo print_r($result);

                $venues =   array_merge($result);

                $venues_array = array('Choose A City');

                $get_venues = $venues;

                foreach ( $get_venues as $venue ) {
                    $venues_array[] .= $venue;
                }

                $venue_id = array_unique($venues_array);

                printf('<select name="%s" class="postform form-control">', esc_attr($venue));

                foreach($get_venues as $venue) {
                    printf( '<option value="%s">%s</option>', esc_attr( $venue), esc_html(eventbrite_event_venue()->address->city ) );
                }

                print('</select>');


            endwhile;  

        endif;

        wp_reset_postdata();
        wp_reset_query(); 

        ?>

        </select>
    </form>
</div>

以下代码

              $result =  array(eventbrite_event_venue());
               echo print_r($result);

输出以下内容:

Array
 (
   [0] =&gt; stdClass Object
    (
        [address] =&gt; stdClass Object
            (
                [address_1] =&gt; 350 5th Avenue,
                [address_2] =&gt; 5th Floor, The LAB
                [city] =&gt; New York
                [region] =&gt; NY
                [postal_code] =&gt; 10118
                [country] =&gt; US
                [latitude] =&gt; 40.7353956
                [longitude] =&gt; -73.99462499999998
                [localized_address_display] =&gt; 350 5th Avenue, 5th 
                Floor, The LAB, New York, NY 10118
                [localized_area_display] =&gt; New York, NY
                [localized_multi_line_address_display] =&gt; Array
                    (
                        [0] =&gt; 350 5th Avenue
                        [1] =&gt; 5th Floor, The LAB
                        [2] =&gt; New York, NY 10118
                    )

            )

        [resource_uri] =&gt; 
        https://www.eventbriteapi.com/v3/venues/25816848/
        [id] =&gt; 25816848
        [age_restriction] =&gt; 
        [capacity] =&gt; 
        [name] =&gt; Global Brands Group
        [latitude] =&gt; 40.7353956
        [longitude] =&gt; -73.99462499999998
    )

   )
   1Array
   (
[0] =&gt; stdClass Object
    (
        [address] =&gt; stdClass Object
            (
                [address_1] =&gt; 350 5th Avenue,
                [address_2] =&gt; 5th Floor, HR Training Room
                [city] =&gt; New York
                [region] =&gt; NY
                [postal_code] =&gt; 10118
                [country] =&gt; US
                [latitude] =&gt; 40.7484799
                [longitude] =&gt; -73.98542450000002
                [localized_address_display] =&gt; 350 5th Avenue, 5th Floor, HR Training Room, New York, NY 10118
                [localized_area_display] =&gt; New York, NY
                [localized_multi_line_address_display] =&gt; Array
                    (
                        [0] =&gt; 350 5th Avenue
                        [1] =&gt; 5th Floor, HR Training Room
                        [2] =&gt; New York, NY 10118
                    )

            )

        [resource_uri] =&gt; https://www.eventbriteapi.com/v3/venues/25536431/
        [id] =&gt; 25536431
        [age_restriction] =&gt; 
        [capacity] =&gt; 
        [name] =&gt; Global Brands Group
        [latitude] =&gt; 40.7484799
        [longitude] =&gt; -73.98542450000002
    )

   )
   1Array
    (
[0] =&gt; stdClass Object
    (
        [address] =&gt; stdClass Object
            (
                [address_1] =&gt; 350 5th Avenue,
                [address_2] =&gt; 5th Floor, HR Training Room
                [city] =&gt; New York
                [region] =&gt; NY
                [postal_code] =&gt; 10118
                [country] =&gt; US
                [latitude] =&gt; 40.7484799
                [longitude] =&gt; -73.98542450000002
                [localized_address_display] =&gt; 350 5th Avenue, 5th Floor, HR Training Room, New York, NY 10118
                [localized_area_display] =&gt; New York, NY
                [localized_multi_line_address_display] =&gt; Array
                    (
                        [0] =&gt; 350 5th Avenue
                        [1] =&gt; 5th Floor, HR Training Room
                        [2] =&gt; New York, NY 10118
                    )

            )

        [resource_uri] =&gt; https://www.eventbriteapi.com/v3/venues/25536431/
        [id] =&gt; 25536431
        [age_restriction] =&gt; 
        [capacity] =&gt; 
        [name] =&gt; Global Brands Group
        [latitude] =&gt; 40.7484799
        [longitude] =&gt; -73.98542450000002
    )

   )
    1Array
   (
[0] =&gt; stdClass Object
    (
        [address] =&gt; stdClass Object
            (
                [address_1] =&gt; 350 5th Avenue,
                [address_2] =&gt; 5th Floor, HR Training Room
                [city] =&gt; New York
                [region] =&gt; NY
                [postal_code] =&gt; 10118
                [country] =&gt; US
                [latitude] =&gt; 40.7484799
                [longitude] =&gt; -73.98542450000002
                [localized_address_display] =&gt; 350 5th Avenue, 5th Floor, HR Training Room, New York, NY 10118
                [localized_area_display] =&gt; New York, NY
                [localized_multi_line_address_display] =&gt; Array
                    (
                        [0] =&gt; 350 5th Avenue
                        [1] =&gt; 5th Floor, HR Training Room
                        [2] =&gt; New York, NY 10118
                    )

            )

        [resource_uri] =&gt; https://www.eventbriteapi.com/v3/venues/25536431/
        [id] =&gt; 25536431
        [age_restriction] =&gt; 
        [capacity] =&gt; 
        [name] =&gt; Global Brands Group
        [latitude] =&gt; 40.7484799
        [longitude] =&gt; -73.98542450000002
    )

    )
    1Array
    (
[0] =&gt; stdClass Object
    (
        [address] =&gt; stdClass Object
            (
                [address_1] =&gt; 350 5th Avenue,
                [address_2] =&gt; 5th Floor, HR Training Room
                [city] =&gt; New York
                [region] =&gt; NY
                [postal_code] =&gt; 10118
                [country] =&gt; US
                [latitude] =&gt; 40.7484799
                [longitude] =&gt; -73.98542450000002
                [localized_address_display] =&gt; 350 5th Avenue, 5th Floor, HR Training Room, New York, NY 10118
                [localized_area_display] =&gt; New York, NY
                [localized_multi_line_address_display] =&gt; Array
                    (
                        [0] =&gt; 350 5th Avenue
                        [1] =&gt; 5th Floor, HR Training Room
                        [2] =&gt; New York, NY 10118
                    )

            )

        [resource_uri] =&gt; https://www.eventbriteapi.com/v3/venues/25536431/
        [id] =&gt; 25536431
        [age_restriction] =&gt; 
        [capacity] =&gt; 
        [name] =&gt; Global Brands Group
        [latitude] =&gt; 40.7484799
        [longitude] =&gt; -73.98542450000002
    )

       )

我不确定如何将数组合并为一个,当您选择城市时,变量应该进入此处以过滤事件的循环

       <?php
        // Set up and call our Eventbrite query.
        $events = new Eventbrite_Query( apply_filters( 
            'eventbrite_query_args', array(
            'display_private' => true, // boolean
            'status' => 'live',         // string (only available for 
             display_private true)
            'nopaging' => true,        // boolean
            'limit' => 100,            // integer
            //'organizer_id' => $organizerfilter,     // integer
            // 'p' => null,                // integer
            // 'post__not_in' => null,     // array of integers
                'venue_id' => $venue,         // integer
            // 'category_id' => null,      // integer
            // 'subcategory_id' => null,   // integer
            // 'format_id' => null,        // integer
        ) ) );
            if ( $events->have_posts() ) :
            while ( $events->have_posts() ) : $events->the_post(); ?>

            <?php get_template_part( 'template-parts/content', 'events' ); ?>

            <?php endwhile;
            // Previous/next post navigation.
            eventbrite_paging_nav();
        else :
            // If no content, include the "No posts found" template.
            get_template_part( 'content', 'none' );
        endif;
        // Return $post to its rightful owner.
        wp_reset_query();
    ?>

标签: phpwordpressapi

解决方案


推荐阅读