网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

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

GG网络技术分享 2025-03-18 16:12 5


问题描述:

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!

网友观点:

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

}

###

Another way to resolve repeated elements in array is to use 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\\\"

}

}

标签:

提交需求或反馈

Demand feedback