I'm using usort to sort a multi-dimensional array. It works fine until the key used to sort concatenates a global variable.
var_dump($lang);//OK, outputs 'eng'
function cmp(array $a, array $b) {
if ($a['name'] < $b['name']) {
return -1;
} else if ($a['name'] > $b['name']) {
return 1;
} else {
return 0;
}
}
function cmp2(array $a, array $b) {
global $lang;
var_dump($lang);//null?
if ($a['name_'.$lang] < $b['name_'.$lang]) {//line: 93
return -1;
} else if ($a['name_'.$lang] > $b['name_'.$lang]) {
return 1;
} else {
return 0;
}
}
usort($platform['Server'], "cmp");//OK
usort($platform['Asset'], "cmp2");//Notice (8): Undefined index: name_ [APP\View\Platforms\view.ctp, line 93]
How can I concatenate the lang variable inside the function passed to usort?
I'm on PHP 5.3.
Also, would there be a way to unify both these functions into one while passing the key to sort on as a parameter?
$langisn't actually global.