removed create_function
This commit is contained in:
parent
ecb9cb2dfe
commit
a9ac81352e
|
@ -876,90 +876,99 @@ function get_custom_file($index,$filename,$path) {
|
||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$masortSortBy = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sort a multi dimensional array.
|
* Sort a multi dimensional array.
|
||||||
*
|
*
|
||||||
* @param array Multi demension array passed by reference
|
* @param array Multi demension array passed by reference
|
||||||
* @param string Comma delimited string of sort keys.
|
* @param string Comma delimited string of sort keys.
|
||||||
* @param boolean Whether to reverse sort.
|
* @param boolean Whether to reverse sort.
|
||||||
* @return array Sorted multi demension array.
|
|
||||||
*/
|
*/
|
||||||
function masort(&$data,$sortby,$rev=0) {
|
function masort(&$data,$sortby,$rev=false) {
|
||||||
# if the array to sort is null or empty
|
global $masortSortBy;
|
||||||
if (! $data) return;
|
$masortSortBy = explode(',', $sortby);
|
||||||
|
foreach ($masortSortBy as $index => $value) {
|
||||||
|
if (!preg_match('/^[a-zA-z0-9_]+$/', $value)) {
|
||||||
|
unset($masortSortBy[$index]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$masortSortBy = array_values($masortSortBy);
|
||||||
|
uasort($data, 'masortCallback');
|
||||||
|
if ($rev) {
|
||||||
|
$data = array_reverse($data, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$code = "\$c=0;\n";
|
/**
|
||||||
|
* Callback to sort for masort().
|
||||||
foreach (explode(',',$sortby) as $key) {
|
*
|
||||||
if (!preg_match('/^[a-zA-z0-9_]+$/', $key)) {
|
* @param array|object $a first parameter
|
||||||
return;
|
* @param array|object $b second parameter
|
||||||
|
* @return int comparison result
|
||||||
|
*/
|
||||||
|
function masortCallback($a, $b) {
|
||||||
|
global $masortSortBy;
|
||||||
|
foreach ($masortSortBy as $key) {
|
||||||
|
if (is_object($a)) {
|
||||||
|
if (is_array($a->$key)) {
|
||||||
|
asort($a->$key);
|
||||||
|
$aa = array_shift($a->$key);
|
||||||
}
|
}
|
||||||
$code .= "if (is_object(\$a) || is_object(\$b)) {\n";
|
else {
|
||||||
|
$aa = $a->$key;
|
||||||
$code .= " if (is_array(\$a->$key)) {\n";
|
}
|
||||||
$code .= " asort(\$a->$key);\n";
|
if (is_array($b->$key)) {
|
||||||
$code .= " \$aa = array_shift(\$a->$key);\n";
|
asort($b->$key);
|
||||||
$code .= " } else\n";
|
$bb = array_shift($b->$key);
|
||||||
$code .= " \$aa = \$a->$key;\n";
|
}
|
||||||
|
else {
|
||||||
$code .= " if (is_array(\$b->$key)) {\n";
|
$bb = $b->$key;
|
||||||
$code .= " asort(\$b->$key);\n";
|
}
|
||||||
$code .= " \$bb = array_shift(\$b->$key);\n";
|
if ($aa != $bb) {
|
||||||
$code .= " } else\n";
|
return ($aa > $bb ? 1 : -1);
|
||||||
$code .= " \$bb = \$b->$key;\n";
|
}
|
||||||
|
}
|
||||||
$code .= " if (\$aa != \$bb)";
|
else {
|
||||||
if ($rev)
|
$a = array_change_key_case($a);
|
||||||
$code .= " return (\$aa < \$bb ? 1 : -1);\n";
|
$b = array_change_key_case($b);
|
||||||
else
|
|
||||||
$code .= " return (\$aa > \$bb ? 1 : -1);\n";
|
|
||||||
|
|
||||||
$code .= "} else {\n";
|
|
||||||
|
|
||||||
$code .= " \$a = array_change_key_case(\$a);\n";
|
|
||||||
$code .= " \$b = array_change_key_case(\$b);\n";
|
|
||||||
|
|
||||||
$key = strtolower($key);
|
$key = strtolower($key);
|
||||||
|
|
||||||
$code .= " if ((! isset(\$a['$key'])) && isset(\$b['$key'])) return 1;\n";
|
if ((! isset($a[$key])) && isset($b[$key])) {
|
||||||
$code .= " if (isset(\$a['$key']) && (! isset(\$b['$key']))) return -1;\n";
|
return 1;
|
||||||
|
}
|
||||||
|
if (isset($a[$key]) && (! isset($b[$key]))) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if ((isset($a[$key])) && (isset($b[$key]))) {
|
||||||
|
if (is_array($a[$key])) {
|
||||||
|
asort($a[$key]);
|
||||||
|
$aa = array_shift($a[$key]);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$aa = $a[$key];
|
||||||
|
}
|
||||||
|
if (is_array($b[$key])) {
|
||||||
|
asort($b[$key]);
|
||||||
|
$bb = array_shift($b[$key]);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$bb = $b[$key];
|
||||||
|
}
|
||||||
|
|
||||||
$code .= " if ((isset(\$a['$key'])) && (isset(\$b['$key']))) {\n";
|
if ($aa != $bb) {
|
||||||
$code .= " if (is_array(\$a['$key'])) {\n";
|
if (is_numeric($aa) && is_numeric($bb)) {
|
||||||
$code .= " asort(\$a['$key']);\n";
|
return ($aa > $bb ? 1 : -1);
|
||||||
$code .= " \$aa = array_shift(\$a['$key']);\n";
|
}
|
||||||
$code .= " } else\n";
|
else {
|
||||||
$code .= " \$aa = \$a['$key'];\n";
|
if (($c = strcasecmp($aa,$bb)) != 0 ) {
|
||||||
|
return $c;
|
||||||
$code .= " if (is_array(\$b['$key'])) {\n";
|
}
|
||||||
$code .= " asort(\$b['$key']);\n";
|
}
|
||||||
$code .= " \$bb = array_shift(\$b['$key']);\n";
|
}
|
||||||
$code .= " } else\n";
|
}
|
||||||
$code .= " \$bb = \$b['$key'];\n";
|
|
||||||
|
|
||||||
$code .= " if (\$aa != \$bb)\n";
|
|
||||||
$code .= " if (is_numeric(\$aa) && is_numeric(\$bb)) {\n";
|
|
||||||
|
|
||||||
if ($rev)
|
|
||||||
$code .= " return (\$aa < \$bb ? 1 : -1);\n";
|
|
||||||
else
|
|
||||||
$code .= " return (\$aa > \$bb ? 1 : -1);\n";
|
|
||||||
|
|
||||||
$code .= " } else {\n";
|
|
||||||
|
|
||||||
if ($rev)
|
|
||||||
$code .= " if ( (\$c = strcasecmp(\$bb,\$aa)) != 0 ) return \$c;\n";
|
|
||||||
else
|
|
||||||
$code .= " if ( (\$c = strcasecmp(\$aa,\$bb)) != 0 ) return \$c;\n";
|
|
||||||
|
|
||||||
$code .= " }\n";
|
|
||||||
$code .= " }\n";
|
|
||||||
$code .= "}\n";
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
$code .= 'return $c;';
|
|
||||||
|
|
||||||
uasort($data, create_function('$a, $b',$code));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -132,6 +132,26 @@ class PlaFunctionsTest extends PHPUnit_Framework_TestCase {
|
||||||
$this->compareArray($dataWanted, $data);
|
$this->compareArray($dataWanted, $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_masortObjectMultiSort() {
|
||||||
|
$data = array(
|
||||||
|
'a' => (object) ['key1' => '1'],
|
||||||
|
'b' => (object) ['key1' => '5', 'key2' => 3],
|
||||||
|
'c' => (object) ['key1' => '2'],
|
||||||
|
'd' => (object) ['key1' => '5', 'key2' => 1],
|
||||||
|
);
|
||||||
|
|
||||||
|
masort($data, 'key1,key2', 0);
|
||||||
|
|
||||||
|
$dataWanted = array(
|
||||||
|
'a' => (object) ['key1' => '1'],
|
||||||
|
'c' => (object) ['key1' => '2'],
|
||||||
|
'd' => (object) ['key1' => '5', 'key2' => 1],
|
||||||
|
'b' => (object) ['key1' => '5', 'key2' => 3],
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->compareArray($dataWanted, $data);
|
||||||
|
}
|
||||||
|
|
||||||
private function compareArray($dataWanted, $dataNew) {
|
private function compareArray($dataWanted, $dataNew) {
|
||||||
$this->assertEquals(sizeof($dataWanted), sizeof($dataNew));
|
$this->assertEquals(sizeof($dataWanted), sizeof($dataNew));
|
||||||
$keysWanted = array_keys($dataWanted);
|
$keysWanted = array_keys($dataWanted);
|
||||||
|
|
Loading…
Reference in New Issue