如何在php中为具有类似值的数组分组JSON输出
问题描述:
I am creating an array with WordPress data:
// get list of tags
$custom_terms = get_terms('post_tag');
$term_all = array();
$cats_all = array();
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
while($loop->have_posts()) : $loop->the_post();
$categories = get_the_category();
// create the array
$cats_all[] = array(
'term_name'=>$custom_term->name,
'category_details'=>array(
'category_ID'=> $categories[0]->term_id,
'category_name' => $categories[0]->name,
),
);
endwhile;
}
}
and when outputting a jason, I get this result:
{
"status": "ok",
"all_tags": [
{
"term_name": "Tag 1",
"category_details": {
"category_ID": 7,
"category_name": "category 3"
}
},
{
"term_name": "Tag 1",
"category_details": {
"category_ID": 6,
"category_name": "category 2"
}
},
{
"term_name": "Tag 1",
"category_details": {
"category_ID": 5,
"category_name": "category 1"
}
},
{
"term_name": "Tag 2",
"category_details": {
"category_ID": 8,
"category_name": "category 4"
}
}
]
}
but as you can see, Tag 1 has many categories so I wanted all categories to be under a single tag name. Something like this:
{
"status": "ok",
"all_tags": [
{
"term_name": "Tag 1",
"category_details": [
{
"category_ID": 7,
"category_name": "category 3"
},
{
"category_ID": 6,
"category_name": "category 2"
},
{
"category_ID": 5,
"category_name": "category 1"
}
],
},
{
"term_name": "Tag 2",
"category_details": [
{
"category_ID": 8,
"category_name": "category 4"
},
]
}
]
}
I can't seem to find a way to display the data like this. Can someone please explain how to get this result?
答
You can recode your snippet like bellow with key index is slug of tags
// get list of tags
$custom_terms = get_terms('post_tag');
$term_all = array();
$cats_all = array();
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
while($loop->have_posts()) : $loop->the_post();
$categories = get_the_category();
if( !isset( $cats_all[ $custom_term->slug ] ) ){
// create the array
$cats_all[ $custom_term->slug ] = array(
'term_name' =>$custom_term->name,
'category_details' => array(
array(
$categories[0]->term_id => $categories[0]->name,
)
),
);
}else{
if( !isset( $cats_all[ $custom_term->slug ]['category_details'][ $categories[0]->term_id ] ) )
$cats_all[ $custom_term->slug ]['category_details'][ $categories[0]->term_id ] = $categories[0]->name;
}
endwhile;
}
}