douhezhan5348 2019-02-28 13:58
浏览 70
已采纳

Wordpress JSON删除重复的Object - functions.php

i'm trying to make a JSON route in WordPress that shows only the categories of the posts, without repeating, what i have so far its this:

add_action( 'rest_api_init', 'register_route_cat' ); 
function register_route_cat() {
    register_rest_route( 
        'lojas/v1',
        '/linha/(?P<stringvar>.+)',
        array(
            'methods' => 'GET',
            'callback' => 'get_categories_map',
        )
    );
}

function get_categories_map( $data ) {
  // get the posts type loja
    $posts_list = get_posts( array(
        'post_type' => 'loja',
        'posts_per_page' => 50,
        'post_status'    => 'publish'
    ));

    $post_data = array();


    foreach( $posts_list as $posts) {

        $post_id = $posts->ID;
        $post_title = $posts->post_title;
        $post_estacao = wp_get_post_terms($post_id, 'estacao', array("fields" => "names"));
        $post_cat = wp_get_post_terms($post_id, 'categoria_loja', array("fields" => "names"));
        $cat_imploded = implode(', ', $post_cat);



        $values = array(
                    'id' =>  $cat_imploded,
                    'title' =>  $cat_imploded,
                    'color' =>  $cat_imploded,
                );


        if ($post_estacao[0] == $linha_select  && $cat_imploded != NULL ) {

            array_push($post_data, $values);

        }


    }

    return  $post_data;

}

and it gives me this:

https://i.stack.imgur.com/KvHNe.png

how can i remove the repeated objects?

Thanks!

  • 写回答

2条回答 默认 最新

  • dpbvpgvrhwxen3222 2019-02-28 14:07
    关注

    You can take advantage of PHP's associative arrays to guarantee uniqueness.

    You should make the following changes:

    // array_push($post_data, $values);
    $post_data[ $cat_imploded ] = $values;
    
    // return  $post_data;
    return array_values($post_data);
    

    So get_categories_map() code should look like this:

    function get_categories_map( $data ) {
      // get the posts type loja
        $posts_list = get_posts( array(
            'post_type' => 'loja',
            'posts_per_page' => 50,
            'post_status'    => 'publish'
        ));
    
        $post_data = array();
    
    
        foreach( $posts_list as $posts) {
    
            $post_id = $posts->ID;
            $post_title = $posts->post_title;
            $post_estacao = wp_get_post_terms($post_id, 'estacao', array("fields" => "names"));
            $post_cat = wp_get_post_terms($post_id, 'categoria_loja', array("fields" => "names"));
            $cat_imploded = implode(', ', $post_cat);
    
    
    
            $values = array(
                        'id' =>  $cat_imploded,
                        'title' =>  $cat_imploded,
                        'color' =>  $cat_imploded,
                    );
    
    
            if ($post_estacao[0] == $linha_select  && $cat_imploded != NULL ) {
    
                // Build an associative array to ensure that only one instance of $cat_imploded exists in the array
                $post_data[ $cat_imploded ] = $values;
    
            }
    
    
        }
    
        // Use array_values() to convert the associative array into an indexed array
        return array_values($post_data);
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?