I've the next functions. Wordpress functions, but this is actually a PHP question. They sort my $term objects based on the artist_lastname property in every object's metadata.

I wish to pass a string into $meta within the first function. This could allow me to reuse this code when i could put it on to numerous metadata qualities.

However I don't undertstand the way i can pass extra parameters towards the usort callback. I attempted to create a JS style anonymous function however the PHP version around the server is simply too old and put a syntax error.

Any help - or perhaps a shove for the right corner from the manual - gratefully appreciated. Thanks!

function sort_by_term_meta($terms, $meta) 
{
  usort($terms,"term_meta_cmp");
}

function term_meta_cmp( $a, $b ) 
{
    $name_a = get_term_meta($a->term_id, 'artist_lastname', true);
    $name_b = get_term_meta($b->term_id, 'artist_lastname', true);
    return strcmp($name_a, $name_b); 
} 

This will not assist you to whatsoever with $meta = array('hello'); //define meta in global function term_meta_cmp($a, $b) { global $meta; //access meta data // do comparison here } usort($terms, 'term_meta_cmp'); but may be useful nonetheless. You can sort the array using among the other sorting functions, [cde].

The concept would be to build a range of the values that you'd be sorting on (the return values from usort()) and multisort that against your primary array_multisort() array.

get_term_meta()

Presuming you've use of objects and static (PHP 5 or greater), you may create an item and pass the arguments directly there, like so:

function term_meta_cmp( $a, $b, $meta )  {
    echo "$a, $b, $meta<hr>"; // Debugging output
}
$terms = array("d","c","b","a");
usort($terms, create_function('$a, $b', 'return term_meta_cmp($a, $b, "some-meta");'));

Presuming you do not have use of objects/static you can just perform a global:

<?php
class SortWithMeta {
    private static $meta;

    static function sort(&$terms, $meta) {
       self::$meta = $meta;
       usort($terms, array("SortWithMeta", "cmp_method"));
    }

    static function cmp_method($a, $b) {
       $meta = self::$meta; //access meta data
       // do comparison here
    }

}

// then call it
SortWithMeta::sort($terms, array('hello'));

The paperwork state that [cde] should focus on PHP >= 4..1. All of this?

create_function()

In PHP, one choice for a callback would be to pass a 2-element array that contains an item handle along with a method title to ask the item. For instance, if $obj was a clear case of class MyCallable, and you need to call the method1 approach to MyCallable on $obj, you'll be able to pass array($obj, "method1") like a callback.

One solution by using this supported callback type would be to define just one-use class that basically functions just like a closure type:

function sort_by_term_meta( $terms, $meta ) 
{
    usort($terms, array(new TermMetaCmpClosure($meta), "call"));
}

function term_meta_cmp( $a, $b, $meta )
{
    $name_a = get_term_meta($a->term_id, $meta, true);
    $name_b = get_term_meta($b->term_id, $meta, true);
    return strcmp($name_a, $name_b); 
} 

class TermMetaCmpClosure
{
    private $meta;

    function __construct( $meta ) {
        $this->meta = $meta;
    }

    function call( $a, $b ) {
        return term_meta_cmp($a, $b, $this->meta);
    }
}