重新排序和分组现有阵列

时间:2017-08-23 09:42:10

标签: php arrays json wordpress advanced-custom-fields

我有一个JSON数组,格式如下,在PHP中使用foreach循环生成,从WP ACF组生成一个数组(见下文)。在获取此ACF组的特定字段时,我似乎无法使用GROUP BY,因此显示了名称的双重条目(具有不同的时间)。

[
    {"name":"Location1","gigs": 
        {"start":"16:30","end":"21:15","title":"TestTitle1","class":"fri"}
    },
    {"name":"Location 2","gigs":
        {"start":"15:00","end":"19:00","title":"TestTitle2","class":"fri"}
    },
    {"name":"Location 1","gigs":
        {"start":"21:00","end":"22:00","title":"TestTitle3","class":"fri"}
    }
]

理想情况下,此JSON数组的格式应如下所示,区别在于具有相同名称/位置的条目应组合如下所示。

[
    {
        "name":"Location1",
        "gigs": [
            {"start":"16:30","end":"21:15","title":"TestTitle1","class":"fri"},
            {"start":"21:00","end":"22:00","title":"TestTitle3","class":"fri"}
        ]
    },
    {
        "name":"Location 2",
        "gigs": [
            {"start":"15:00","end":"19:00","title":"TestTitle2","class":"fri"}
        ]
    }
]

我已尝试过PHP中可用的多个数组合并/合并/排序/等功能,但无法按预期输出。

这是生成数组/ JSON输出的代码:

function get_shows( WP_REST_Request $request ) {
    $parameters = $request->get_params();

    switch($parameters[1]) {
        case 'fri':
            $show_day = 'fri';
            $show_select_day = '20170901';
            break;
        case 'sat':
            $show_day = 'sat';
            $show_select_day = '20170902';
            break;
        case 'sun':
            $show_day = 'sun';
            $show_select_day = '20170903';
            break;
    }

    $showDates   = array();
    $args = array(
        'posts_per_page'    => -1,
        'post_type'         => 'performances',
        'meta_key'          => 'per_date',
        'meta_value'        => $show_select_day
    );

    $shows = get_posts( $args );

    if ( ! empty( $shows ) ) {
        $i = 0;
        foreach( $shows as $show ) {
            $acf = get_fields($show->ID);
            $show_loc = get_the_title(get_field('per_location',$show->ID));
            $show_title = get_the_title(get_field('per_busker',$show->ID));

            if ( ! empty( $acf ) ) {
                $showDates[$i]['name'] = $show_loc;
                $showDates[$i]['gigs']['start']     = date("H:i", strtotime($acf['per_time_start']));
                $showDates[$i]['gigs']['end']   = date("H:i", strtotime($acf['per_time_end']));
                $showDates[$i]['gigs']['title']     = $show_title;
                $showDates[$i]['gigs']['class']     = $show_day;

            }
            $i++;
        }
    }
   return   $showDates;
}

任何以正确格式获得输出的建议都非常受欢迎。非常感谢!

1 个答案:

答案 0 :(得分:0)

尝试以下解决方案。我已将您的密钥import sqlContext.implicits._ DF1.alias("a").join(DF2.alias("b"),DF1("primary_col") === DF2("primary_col"), "inner").select($"a.col1".alias("first"),$"a.col2".alias("second"),$"b.col4".alias("third")) 更改为import sqlContext.implicits._ DF1.alias("a").join(DF2.alias("b"),DF1("primary_col") === DF2("primary_col"), "inner").select($"a.col1", $"a.col2", $"b.col4".alias("third")) ,因为Location1

Location 1

DEMO