dongtang4954 2016-11-02 03:57
浏览 58
已采纳

如何在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 2016-11-02 04:49
    关注

    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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用