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

报告相同问题?

悬赏问题

  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU