0

我已经阅读了一些不同的主题(比如这个和关于uasort()的一般文档信息- 我相信这是我正在寻找的),但数组仍然有点固执(在我的使用中 -案子)。

这是我所拥有的:

// This sets an array of values to a variable
$collection_rows = get_field('collection_profiles');

print_r$collection_rows

Array
(
    [0] => Array
        (
            [collection_profile] => Array
                (
                    [0] => stdClass Object
                        (
                            [ID] => 273
                            [post_author] => 1
                            [post_date] => 2012-03-26 07:53:45
                            [post_date_gmt] => 2012-03-26 13:53:45
                            [post_content] => 
                            [post_title] => Profile 1
                            [post_excerpt] => 
                            [post_status] => publish
                            [comment_status] => closed
                            [ping_status] => closed
                            [post_password] => 
                            [post_name] => profile-1
                            [to_ping] => 
                            [pinged] => 
                            [post_modified] => 2012-04-12 08:07:35
                            [post_modified_gmt] => 2012-04-12 14:07:35
                            [post_content_filtered] => 
                            [post_parent] => 0
                            [guid] => http://domain.com/?post_type=moulding_profiles&p=273
                            [menu_order] => 0
                            [post_type] => moulding_profiles
                            [post_mime_type] => 
                            [comment_count] => 0
                            [ancestors] => Array
                                (
                                )

                            [filter] => raw
                        )

                )

            [collection_profile_note] => 1
        )

    [1] => Array
        (
            [collection_profile] => Array
                (
                    [0] => stdClass Object
                        (
                            [ID] => 188
                            [post_author] => 1
                            [post_date] => 2012-02-17 15:24:24
                            [post_date_gmt] => 2012-02-17 21:24:24
                            [post_content] => 
                            [post_title] => Test Profile
                            [post_excerpt] => 
                            [post_status] => publish
                            [comment_status] => closed
                            [ping_status] => closed
                            [post_password] => 
                            [post_name] => test-profile
                            [to_ping] => 
                            [pinged] => 
                            [post_modified] => 2012-02-28 14:13:32
                            [post_modified_gmt] => 2012-02-28 20:13:32
                            [post_content_filtered] => 
                            [post_parent] => 0
                            [guid] => http://domain.com/?post_type=moulding_profiles&p=188
                            [menu_order] => 0
                            [post_type] => moulding_profiles
                            [post_mime_type] => 
                            [comment_count] => 0
                            [ancestors] => Array
                                (
                                )

                            [filter] => raw
                        )

                )

            [collection_profile_note] => 3
        )

    [2] => Array
        (
            [collection_profile] => Array
                (
                    [0] => stdClass Object
                        (
                            [ID] => 207
                            [post_author] => 1
                            [post_date] => 2012-02-23 13:35:55
                            [post_date_gmt] => 2012-02-23 19:35:55
                            [post_content] => 
                            [post_title] => Casing Test Profile
                            [post_excerpt] => 
                            [post_status] => publish
                            [comment_status] => closed
                            [ping_status] => closed
                            [post_password] => 
                            [post_name] => casing-test-profile
                            [to_ping] => 
                            [pinged] => 
                            [post_modified] => 2012-02-23 13:35:55
                            [post_modified_gmt] => 2012-02-23 19:35:55
                            [post_content_filtered] => 
                            [post_parent] => 0
                            [guid] => http://domain.com/?post_type=moulding_profiles&p=207
                            [menu_order] => 0
                            [post_type] => moulding_profiles
                            [post_mime_type] => 
                            [comment_count] => 0
                            [ancestors] => Array
                                (
                                )

                            [filter] => raw
                        )

                )

            [collection_profile_note] => 2
        )

)

(相当笨拙)

我正在寻找按数组键/值排序collection_profile_note。我尝试过的(到目前为止)是:

$collection_rows = get_field('collection_profiles');
print_r($collection_rows);
if ($collection_rows) {
    echo '<h2>'.__('Profiles in Collection','roots').'</h2>';
    echo '<ul>';
    function cmp($a, $b) {
        if ($a->collection_profile_note == $b->collection_profile_note) {
            return 0;
        } else {
            return $a->collection_profile_note < $b->collection_profile_note ? 1 : -1;
        }
    }
    usort($collection_rows, 'cmp');

    foreach($collection_rows as $collection_row) {
        // Extract single post value
        $collection_profile_field = $collection_row['collection_profile'];
        $collection_profile_page = isset($collection_profile_field[0]) ? $collection_profile_field[0]->ID : NULL;
        ?>
        <li><a href="<?php echo get_permalink($collection_profile_page); ?>"><?php echo get_the_title($collection_profile_page); ?></a> <?php echo $collection_row['collection_profile_note']; ?></li>
    <?php }
    echo '</ul>';
}

虽然它确实改变了没有它显示的顺序,但uasort()它并没有按照我想要的方式排列它们 - > 使用函数 (2, 3, 1),没有函数 (1, 3, 2)

任何帮助将不胜感激。谢谢!

4

2 回答 2

0

好吧,您已经向后实现了排序逻辑:

function cmp($a, $b) {
    if ($a->collection_profile_note == $b->collection_profile_note) {
        return 0;
    } else {
        return $a->collection_profile_note < $b->collection_profile_note ? 1 : -1;
    }
}

文档中uasort()

function cmp($a, $b) {
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}

请注意,您正在使用此逻辑:

  • 如果a < b,则返回1(本质上是告诉排序函数a > b)
  • 如果a > b,则返回-1(本质上是告诉排序函数a < b)
于 2012-04-16T21:21:24.530 回答
0

你有必要对数组进行排序吗?分解您正在排序的键和值可能更容易,对它们进行排序,然后遍历该数组(像这样):

$sortme=array();
foreach( $collection_rows as $key=>$profile )
{
    $sortme[$key] = $profile['collection_profile_note'];
}

asort($sortme);

foreach( $sortme as $node=>$ignore ) 
{
    print_r($collection_rows[$node]);
}

承认这几乎使排序时间增加了一倍,但也许足以满足您的需求?

于 2012-04-16T21:22:55.977 回答