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 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调