deafdigit
08-16-2012, 04:02 PM
Hi,
I have a multidimentional array (2 dimensions) and I'd like to sort the outer-most array according to a key of the inner-most arrays.
array(5) {
[0]=>
array(3) {
["id"]=>
int(1)
["navn"]=>
string(14) "Navn1"
["num"]=>
int(0)
}
[1]=>
array(3) {
["id"]=>
int(2)
["navn"]=>
string(17) "Navn2"
["num"]=>
int(0)
}
[2]=>
array(3) {
["id"]=>
int(3)
["navn"]=>
string(15) "Navn3"
["num"]=>
int(1)
}
[3]=>
array(3) {
["id"]=>
int(4)
["navn"]=>
string(23) "Navn4"
["num"]=>
int(0)
}
[4]=>
array(3) {
["id"]=>
int(5)
["navn"]=>
string(9) "Navn5"
["num"]=>
int(3)
}
}
I'd like to sort the outer array descending in regards to the "num"-key (so that "Mads Jensen" becomes the first item.
I found this function online:
function subval_sort($a,$subkey) {
foreach($a as $k=>$v) {
$b[$k] = strtolower($v[$subkey]);
}
asort($b);
foreach($b as $key=>$val) {
$c[] = $a[$key];
}
return $c;
}
Problem is it doesn't seem to do anything - nothing changes in the array.
Can anybody tell me why, or preferably "fix my function"?
Thanks in advance,
Deaf_digit
I have a multidimentional array (2 dimensions) and I'd like to sort the outer-most array according to a key of the inner-most arrays.
array(5) {
[0]=>
array(3) {
["id"]=>
int(1)
["navn"]=>
string(14) "Navn1"
["num"]=>
int(0)
}
[1]=>
array(3) {
["id"]=>
int(2)
["navn"]=>
string(17) "Navn2"
["num"]=>
int(0)
}
[2]=>
array(3) {
["id"]=>
int(3)
["navn"]=>
string(15) "Navn3"
["num"]=>
int(1)
}
[3]=>
array(3) {
["id"]=>
int(4)
["navn"]=>
string(23) "Navn4"
["num"]=>
int(0)
}
[4]=>
array(3) {
["id"]=>
int(5)
["navn"]=>
string(9) "Navn5"
["num"]=>
int(3)
}
}
I'd like to sort the outer array descending in regards to the "num"-key (so that "Mads Jensen" becomes the first item.
I found this function online:
function subval_sort($a,$subkey) {
foreach($a as $k=>$v) {
$b[$k] = strtolower($v[$subkey]);
}
asort($b);
foreach($b as $key=>$val) {
$c[] = $a[$key];
}
return $c;
}
Problem is it doesn't seem to do anything - nothing changes in the array.
Can anybody tell me why, or preferably "fix my function"?
Thanks in advance,
Deaf_digit