将GET_Categories数组与另一个数组进行比较

时间:2015-08-26 作者:Georgi Demirev

我有一个函数,可以从每个类别中提取最新的帖子,并按最新的帖子排列类别。我从中得到的是一个带有类别ID的数组,我想用它来重新排列get\\u categories数组。

/* Get all categories */
$categories = get_categories();
/* Create empty array */
$categories_order = [];
/* For each category */
foreach($categories as $category) {
  /* Modify WP query */
  $args = array(\'posts_per_page\' => 1, /* Max 1 post */
                \'category__in\' => array($category->term_id), /* In this specific category */
                \'ignore_sticky_posts\' => true ); /* No sticky posts */
  /* Get all posts from categories with modifier */
  $posts = get_posts($args);
  /* If there are posts */
  if ($posts) {
    /* For each post */
    foreach($posts as $post) {
      /* Add to array key => value (category id => time published) */
      $categories_order[$category->term_id] = get_post_time(\'YmdHis\');
    }
  }
}
arsort($categories_order); /* Order new array by value */
$categories_order = array_keys($categories_order); /* Remove array values */
print_r($categories_order);
My函数返回:

Array ( [0] => 2 [1] => 5 [2] => 4 )

get\\u categories返回:

Array
(
    [1] => stdClass Object
        (
            [term_id] => 2
            [name] => Спорт
            [slug] => sport
            [term_group] => 0
            [term_taxonomy_id] => 2
            [taxonomy] => category
            [description] => 
            [parent] => 0
            [count] => 6
            [cat_ID] => 2
            [category_count] => 6
            [category_description] => 
            [cat_name] => Спорт
            [category_nicename] => sport
            [category_parent] => 0
        )

    [2] => stdClass Object
        (
            [term_id] => 4
            [name] => Дом и градина
            [slug] => home-and-garden
            [term_group] => 0
            [term_taxonomy_id] => 4
            [taxonomy] => category
            [description] => 
            [parent] => 0
            [count] => 1
            [cat_ID] => 4
            [category_count] => 1
            [category_description] => 
            [cat_name] => Дом и градина
            [category_nicename] => home-and-garden
            [category_parent] => 0
        )

    [3] => stdClass Object
        (
            [term_id] => 5
            [name] => Транспорт
            [slug] => transport
            [term_group] => 0
            [term_taxonomy_id] => 5
            [taxonomy] => category
            [description] => 
            [parent] => 0
            [count] => 1
            [cat_ID] => 5
            [category_count] => 1
            [category_description] => 
            [cat_name] => Транспорт
            [category_nicename] => transport
            [category_parent] => 0
        )

)
现在不知何故,get_categories term_id应该与我的数组进行比较,并相应地重新排列对象。

2 个回复
最合适的回答,由SO网友:TheDeadMedic 整理而成

长话短说,使用按ID索引的类别构建一个新数组。然后,您可以循环使用按日期排序的ID,并根据需要使用它们:

$categories = get_categories();
$cats_index =
$cats_dates = array();

foreach( $categories as $category ) {
    // Index the categories by their ID
    $cats_index[ $category->term_id ] = $category;

    $posts = get_posts(
        array(
            \'ignore_sticky_posts\' => true,
            \'posts_per_page\' => 1,
            \'category__in\' => array( $category->term_id ),
        )
    );

    if ( $posts )
        $cats_dates[ $category->term_id ] = ( int ) mysql2date( \'U\', $posts[0]->post_date_gmt );
    else
        $cats_dates[ $category->term_id ] = 0;
}

arsort( $cats_dates );

foreach ( array_keys( $cats_dates ) as $cat_id ) {
    // Loop over category IDs by date descending
    $category = $cats_index[ $cat_id ];
}

SO网友:JHoffmann

usort 使用回调函数对数组排序:

$categories_order = array_flip($categories_order);
$categories = usort( get_categories(), function( $a, $b ) {
    return $categories_order( $a[\'term_id\'] ) - $categories_order( $b[\'term_id\'] );
}