如何在php中对JSON输出具有相似值的数组进行分组


How to group arrays with similar values in php for JSON output

我正在创建一个WordPress数据数组:

  // 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;
        }
   }

,当输出jason时,我得到这样的结果:

{
  "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有许多类别,所以我希望所有类别都在一个标签名称下。像这样:

{
  "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"
        },
      ]
    }
  ]
}

我似乎找不到这样显示数据的方法。有人能解释一下如何得到这个结果吗?

你可以像下面这样重新编码你的代码片段,关键索引是标签的段塞

// 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;
    }
}