dongtang4954
dongtang4954
2016-11-02 03:57

如何在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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dqpwdai095465 dqpwdai095465 5年前

    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;
        }
    }
    
    点赞 评论 复制链接分享

为你推荐