This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
Deon George 130a87aa9a Major work to domain and hosting
Minor updates for ADSL services
Updates to Sort::MAsort()
Move core OSB items under application/
Moved ACCOUNT functions under application
Minor updates to task
2012-01-12 19:53:55 +11:00

91 lines
2.5 KiB
PHP

<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class is used to sort multiple dimension arrays.
*
* @package lnApp
* @subpackage Sort
* @category Helpers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
* @uses Style
*/
class lnApp_Sort {
/**
* Sort a multi dimensional array.
*
* @param array Multi demension array passed by reference
* @param string Comma delimited string of sort keys.
* @param boolean Whether to reverse sort.
* @return array Sorted multi demension array.
*/
public static function MAsort(&$data,$sortby,$rev=0) {
// if the array to sort is null or empty, or our sortby is bad
if (! preg_match('/^[a-zA-Z0-9_]+(\([a-zA-Z0-9_,]*\))?$/',$sortby) || ! $data)
return;
$code = '$c=0;';
foreach (explode(',',$sortby) as $key) {
$code .= 'if (is_object($a) || is_object($b)) {';
foreach (array('a','b') as $x) {
$code .= 'if (is_array($'.$x.'->'.$key.')) {';
$code .= 'asort($'.$x.'->'.$key.');';
$code .= '$x'.$x.' = array_shift($'.$x.'->'.$key.');';
$code .= '} else';
$code .= '$x'.$x.' = $'.$x.'->'.$key.';';
}
$code .= 'if ($xa != $xb)';
if ($rev)
$code .= 'return ($xa < $xb ? 1 : -1);';
else
$code .= 'return ($xa > $xb ? 1 : -1);';
$code .= '} else {';
foreach (array('a','b') as $x)
$code .= '$'.$x.' = array_change_key_case($'.$x.');';
$key = strtolower($key);
$code .= 'if ((! isset($a[\''.$key.'\'])) && isset($b[\''.$key.'\'])) return 1;';
$code .= 'if (isset($a[\''.$key.'\']) && (! isset($b[\''.$key.'\']))) return -1;';
$code .= 'if ((isset($a[\''.$key.'\'])) && (isset($b[\''.$key.'\']))) {';
foreach (array('a','b') as $x) {
$code .= 'if (is_array($'.$x.'[\''.$key.'\'])) {';
$code .= 'asort($'.$x.'[\''.$key.'\']);';
$code .= '$x'.$x.' = array_shift($'.$x.'[\''.$key.'\']);';
$code .= '} else';
$code .= '$x'.$x.' = $'.$x.'[\''.$key.'\'];';
}
$code .= 'if ($xa != $xb)';
$code .= 'if (is_numeric($xa) && is_numeric($xb)) {';
if ($rev)
$code .= 'return ($xa < $xb ? 1 : -1);';
else
$code .= 'return ($xa > $xb ? 1 : -1);';
$code .= '} else {';
if ($rev)
$code .= 'if (($c = strcasecmp($xb,$xa)) != 0) return $c;';
else
$code .= 'if (($c = strcasecmp($xa,$xb)) != 0) return $c;';
$code .= '}}}';
}
$code .= 'return $c;';
$result = create_function('$a, $b',$code);
uasort($data,$result);
}
}
?>