简体   繁体   English

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

[英]How to sort results (by name) of an array?

I am using which code which returns taxonomies which match 2 values. 我正在使用哪个代码返回与2个值匹配的分类法。

Everything works as it should, but I can't figure out how to order my results. 一切正常,但我不知道如何排序结果。 Right now they are displayed in some set order, it might be date not sure. 现在,它们以一定的顺序显示,可能不确定日期。 I am trying to get them to display alphabetically (by name).. 我正在尝试让它们按字母顺序显示(按名称)。

My Code from my php template is pasted here http://pastie.org/5083124 我的php模板中的代码粘贴在这里http://pastie.org/5083124

The array I am talking about is this 我在说的数组是这个

<?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

?>

I have looked at a few different options with $args and ksort, but I get a bit lost and can't seem to get my results on the frontend of the site to be sorted alphabetically. 我用$ args和ksort看了几个不同的选项,但是我有点迷茫,似乎无法在网站前端得到按字母顺序排序的结果。

Can anyone identify in my code how I would be able to have my results have a sort order? 谁能在我的代码中确定如何使我的结果具有排序顺序?

Thanks 谢谢

You can avoid bothering to sort in the PHP by sorting slightly earlier, when you're querying the database. 在查询数据库时,可以通过稍早排序来避免在PHP中进行排序。 This should be faster. 这应该更快。

Change: 更改:

$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");

...to: ...至:

$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");

ie just add ORDER BY name to your original query. 即只是将ORDER BY name添加到原始查询中。 The results will be returned sorted by name with no need for you to do anything further in the PHP, and the sort will happen on the database server. 结果将返回按名称排序的结果,而您无需在PHP中进行任何进一步的操作,排序将在数据库服务器上进行。 The WordPress database table terms has an index on the name column, so this should be very fast; WordPress数据库表termsname列上有一个索引,因此这应该非常快。 effectively the data is pre-sorted for you. 有效地为您预先整理了数据。 (See the description of the terms table in the WP database schema .) (请参阅WP数据库模式中的terms描述 。)

Have a look at the examples in http://php.net/manual/en/function.sort.php 看一下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";
}

The above example will output: 上面的示例将输出:

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

This is possible using usort and your own comparison function: 使用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 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM