<?php

show_source(__FILE__);

Function multi_array_sort(&$arr, $sortby, $order=-1) {
  if ($order != 1) $order = -1;
  $sortfunc = create_function('$a,$b', "\$a1=\$a$sortby;\$b1=\$b$sortby; if (\$a1==\$b1) return 0; else return (\$a1<\$b1) ? $order : 0- $order;");
  uasort($arr, $sortfunc);
}
Function multi_array_sorti(&$arr, $sortby, $order=-1) {
  if ($order != 1) $order = -1;
  $sortfunc = create_function('$a,$b', "\$a1=strtolower(\$a$sortby);\$b1=strtolower(\$b$sortby); if (\$a1==\$b1) return 0; else return (\$a1<\$b1) ? $order : 0- $order;");
  uasort($arr, $sortfunc);
}

$myarray = array(
  'blah' => array ('colour'=>'red', 'order'=>3),
  'foo' => array ('colour'=>'blue', 'order'=>1),
  'bar' => array ('colour'=>'Yellow', 'order'=>2),
  );

echo '<pre>BEFORE sort: ', print_r($myarray, true), '</pre>';
#multi_array_sort($myarray, '[order]');
multi_array_sorti($myarray, '[colour]');
echo '<pre>AFTER sort: ', print_r($myarray, true), '</pre>';


# More complicated

$myarray = array(
  'blah' => array ('colour'=>'red', 'something'=>array('boo'=>1, 'order'=>3)),
  'foo' => array ('colour'=>'blue', 'something'=>array('boo'=>1, 'order'=>1)),
  'bar' => array ('colour'=>'yellow', 'something'=>array('boo'=>1, 'order'=>2)),
  );
echo '<hr><pre>BEFORE sort: ', print_r($myarray, true), '</pre>';
multi_array_sort($myarray, '[something][order]', 1);
echo '<pre>AFTER sort: ', print_r($myarray, true), '</pre>';
BEFORE sort: Array
(
    [blah] => Array
        (
            [colour] => red
            [order] => 3
        )

    [foo] => Array
        (
            [colour] => blue
            [order] => 1
        )

    [bar] => Array
        (
            [colour] => Yellow
            [order] => 2
        )

)

Fatal error: Uncaught Error: Call to undefined function create_function() in /var/www/pgregg.com/source/projects/php/multi_array_sort/multi_array_sort.inc.php:12 Stack trace: #0 /var/www/pgregg.com/source/projects/php/multi_array_sort/multi_array_sort.inc.php(24): multi_array_sorti(Array, '[colour]') #1 {main} thrown in /var/www/pgregg.com/source/projects/php/multi_array_sort/multi_array_sort.inc.php on line 12