Wordpress JSON删除重复的对象-functions.php

时间:2019-02-28 13:58:58

标签: php wordpress

我正在尝试在WordPress中创建一个JSON路由,该路由仅显示帖子的类别,而不再重复我到目前为止所看到的内容:

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;

}

它给了我这个:

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

如何去除重复的物体?

谢谢!

2 个答案:

答案 0 :(得分:0)

您可以利用PHP的关联数组来保证唯一性。

您应该进行以下更改:

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

// return  $post_data;
return array_values($post_data);

因此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 ) {

            // 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 :(得分:0)

解析数组中重复元素的另一种方法是使用array_unique:

$array= array(
              array(
                    'id' =>  1,
                    'title' =>  'title1',
                    'color' =>  'color1',
                ),
              array(
                    'id' =>  1,
                    'title' =>  'title1',
                    'color' =>  'color1',
            ),
              array(
                    'id' =>  2,
                    'title' =>  'title2',
                    'color' =>  'color2',
                )
    );

$res=array_unique($array,SORT_REGULAR );

var_dump($res);

//output

array(2) {
  [0]=>
  array(3) {
    ["id"]=>
    int(1)
    ["title"]=>
    string(6) "title1"
    ["color"]=>
    string(6) "color1"
  }
  [2]=>
  array(3) {
    ["id"]=>
    int(2)
    ["title"]=>
    string(6) "title2"
    ["color"]=>
    string(6) "color2"
  }
}