如何对数组的结果(按名称)进行排序?

时间:2012-10-19 09:05:10

标签: php arrays wordpress sorting

我正在使用返回与2个值匹配的分类法的代码。

一切都按预期工作,但我无法弄清楚如何订购我的结果。现在它们以某种设定顺序显示,可能是日期不确定。我试图让它们按字母顺序显示(按名称)..

我的php模板中的代码粘贴在http://pastie.org/5083124

我所说的数组就是这个

<?php
    foreach ( $all_terms as $all_term) {
    //print_r($all_terms);

        $tax_test = get_option('woo_categories_panel_taxonomies_'.$all_term->taxonomy);

            $post_images = array();
            $posts_aray = array();
            $parent_id = $all_term->term_taxonomy_id;
            $term_name = $all_term->name;
            $term_parent = $all_term->parent;
            $term_slug = $all_term->slug;
            $term_id = $all_term->term_id;
            $term_link = get_term_link( $all_term, $all_term->taxonomy );
            $counter_value = $all_term->count;

            ?>
            <div class="childListings">
                <div class="block">
                    <a href="<?php echo $term_link; ?>">
                        <?php
                            $block_counter++;
                         ?>
                    </a>

                    <h2><a href="<?php echo $term_link; ?>"><?php echo $term_name ?> <br/><span>(<?php echo $counter_value; ?> Solicitors)</span></a></h2>

                </div><!-- /.block -->
            </div><!-- /.child Listings-->
            <?php

            if ( $block_counter % 6 == 0 ) {
            ?>
                <div class="fix"></div>
            <?php
            } // End IF Statement   

        // End IF Statement

        ?>
        <?php


    } // End For Loop

?>

我用$ args和ksort查看了几个不同的选项,但是我有点迷失了,似乎无法在网站的前端得到我的结果按字母顺序排序。

任何人都可以在我的代码中识别出我的结果是如何排序的吗?

谢谢

3 个答案:

答案 0 :(得分:4)

当您查询数据库时,可以通过稍早排序来避免在PHP中进行排序。这应该更快。

变化:

$all_terms = $wpdb->get_results("SELECT *  FROM ipt1y7_term_taxonomy,ipt1y7_terms WHERE ipt1y7_term_taxonomy.parent='{$ex[2]}' AND ipt1y7_term_taxonomy.term_id=ipt1y7_terms.term_id");

...为:

$all_terms = $wpdb->get_results("SELECT *  FROM ipt1y7_term_taxonomy,ipt1y7_terms WHERE ipt1y7_term_taxonomy.parent='{$ex[2]}' AND ipt1y7_term_taxonomy.term_id=ipt1y7_terms.term_id ORDER BY ipt1y7_terms.name");

即。只需将ORDER BY name添加到原始查询中即可。结果将按名称排序,不需要您在PHP中进一步执行任何操作,排序将在数据库服务器上进行。 WordPress数据库表termsname列上有一个索引,所以这应该非常快;有效地为您预先排序数据。 (参见description of the terms table in the WP database schema。)

答案 1 :(得分:0)

查看http://php.net/manual/en/function.sort.php

中的示例
$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
    echo "fruits[" . $key . "] = " . $val . "\n";
}

以上示例将输出:

fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange

答案 2 :(得分:0)

使用usort和您自己的比较函数可以实现这一点:

<?php

/**
 * For a bit of testing.
 */
$all_terms = array( );
$names = array( 'foo', 'baz', 'bar', 'qux', 'aaa' );
foreach( $names as $name ) {
    $tmp = new stdClass();
    $tmp->name = $name;
    $all_terms[] = $tmp;
}

/**
 * Here, we do the sorting:
 */
usort( $all_terms, function( $a, $b ) {
    if( $a->name === $b->name ) {
        return 0;
    }
    return ( $a->name < $b->name ) ? -1 : 1;
});

/**
 * And the results:
 */
var_dump( $all_terms );
相关问题