Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何对数组的结果(按名称)排序?_Php_Arrays_Wordpress_Sorting - Fatal编程技术网

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

Php 如何对数组的结果(按名称)排序?,php,arrays,wordpress,sorting,Php,Arrays,Wordpress,Sorting,我使用的代码返回与2个值匹配的分类法 一切正常,但我不知道如何对结果进行排序。现在,它们以某种设定的顺序显示,可能是日期不确定。我正试图让它们按字母顺序(按名称)显示 我的php模板中的代码粘贴在这里 我所说的阵列是这样的 <?php foreach ( $all_terms as $all_term) { //print_r($all_terms); $tax_test = get_option('woo_categories_panel_taxonom

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

一切正常,但我不知道如何对结果进行排序。现在,它们以某种设定的顺序显示,可能是日期不确定。我正试图让它们按字母顺序(按名称)显示

我的php模板中的代码粘贴在这里

我所说的阵列是这样的

<?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查看了一些不同的选项,但是我有点迷路了,似乎无法在网站前端按字母顺序排序

有人能在我的代码中指出我如何能够使我的结果具有排序顺序吗


谢谢

请查看中的示例

上述示例将输出:

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

请看中的示例

上述示例将输出:

fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange
这可以使用和您自己的比较功能:

<?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 );
这可以使用和您自己的比较功能:

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

在查询数据库时,您可以稍微提前排序,从而避免麻烦在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");

i、 e.只需在原始查询中添加
orderbyname
。结果将按名称排序返回,无需在PHP中执行任何进一步操作,排序将在数据库服务器上进行。WordPress数据库表
terms
name
列上有一个索引,因此这应该非常快;实际上,数据是为您预先排序的。(请参阅。)

在查询数据库时,您可以通过稍早排序来避免麻烦在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");

i、 e.只需在原始查询中添加
orderbyname
。结果将按名称排序返回,无需在PHP中执行任何进一步操作,排序将在数据库服务器上进行。WordPress数据库表
terms
name
列上有一个索引,因此这应该非常快;实际上,数据是为您预先排序的。(请参阅。)

您好,谢谢,是的,我之前发现了一个类似的关于水果的示例,但我不知道如何将其应用到上面的代码中。你能给我指一下我的代码的正确方向吗?谢谢,洛蒂也许应该强调,我在数组中的术语并不像你在这个例子中用柠檬、苹果等硬编码。如果你看看我上面的pastie,我在页面顶部匹配了2个分类法,如果这有道理的话。谢谢,是的,我以前在水果中发现了一个类似的例子,但我没能理解如何将其应用到上面的代码中。你能给我指一下我的代码的正确方向吗?谢谢,洛蒂也许应该强调,我在数组中的术语并不是像这个例子中柠檬、苹果等那样硬编码的。如果你看我上面的pastie,我会在页面顶部匹配2个分类法,如果这让老师不知道WordPress允许写你自己的查询。这肯定比我的解决方案好。我不知道WordPress允许编写您自己的查询。这肯定比我的解决方案好。