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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀