Redwall
Redwall

Reputation: 1020

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

I am using which code which returns taxonomies which match 2 values.

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

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.

Can anyone identify in my code how I would be able to have my results have a sort order?

Thanks

Upvotes: 0

Views: 171

Answers (3)

Matt Gibson
Matt Gibson

Reputation: 38238

You can avoid bothering to sort in the PHP by sorting slightly earlier, when you're querying the database. 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");

i.e. just add ORDER BY name to your original query. 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. The WordPress database table terms has an index on the name column, so this should be very fast; effectively the data is pre-sorted for you. (See the description of the terms table in the WP database schema.)

Upvotes: 4

Berry Langerak
Berry Langerak

Reputation: 18859

This is possible using usort and your own comparison function:

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

Upvotes: 0

Simone
Simone

Reputation: 21272

Have a look at the examples in 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

Upvotes: 0

Related Questions