のためのPHPで同様の値を持つグループアレイにどのように:ジェイソンを出力する際私はWordPressのデータを配列作成していJSON出力
// 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;
}
}
をして、私はこの結果を得る:
{
"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"
}
}
]
}
が、ご覧のとおり、タグ1には多くのカテゴリがありますので、すべてのカテゴリを1つのタグ名にしたいと考えていました。このようなもの:
{
"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"
},
]
}
]
}
このようなデータを表示する方法が見つからないようです。誰かがこの結果を得る方法を説明できますか?あなたは、キーインデックスと怒鳴るようなあなたのスニペットを再コーディングすることができます
のスラグであります($ key == "term_name"){$ data [$ key] [] = $ value;} ' – Xorifelse