こんにちは、私は PHP 開発者の初心者です
このヘルプでソートしようとしましたhttp://www.php.net/manual/en/function.usort.php#103722
function usortByArrayKey($key, $asc=SORT_ASC) {
$sort_flags = array(SORT_ASC, SORT_DESC);
if (!in_array($asc, $sort_flags))
throw new InvalidArgumentException('sort flag only accepts SORT_ASC or SORT_DESC');
return function(array $a, array $b) use ($key, $asc, $sort_flags) {
if (!is_array($key)) { //just one key and sort direction
if (!isset($a[$key]) || !isset($b[$key])) {
throw new Exception('attempting to sort on non-existent keys');
}
if ($a[$key] == $b[$key])
return 0;
return ($asc == SORT_ASC xor $a[$key] < $b[$key]) ? 1 : -1;
} else { //using multiple keys for sort and sub-sort
foreach ($key as $sub_key => $sub_asc) {
//array can come as 'sort_key'=>SORT_ASC|SORT_DESC or just 'sort_key', so need to detect which
if (!in_array($sub_asc, $sort_flags)) {
$sub_key = $sub_asc;
$sub_asc = $asc;
}
//just like above, except 'continue' in place of return 0
if (!isset($a[$sub_key]) || !isset($b[$sub_key])) {
throw new Exception('attempting to sort on non-existent keys');
}
if ($a[$sub_key] == $b[$sub_key])
continue;
return ($sub_asc == SORT_ASC xor $a[$sub_key] < $b[$sub_key]) ? 1 : -1;
}
return 0;
}
};
}
問題は、私はPHP 5.2を使用していて、次のようにコードを変更することです
function usortByArrayKey(&$array, $key, $asc=SORT_ASC) {
$sort_flags = array(SORT_ASC, SORT_DESC);
if(!in_array($asc, $sort_flags)) {
throw new InvalidArgumentException('sort flag only accepts SORT_ASC or SORT_DESC');
}
$cmp = $this->_usortByArrayKey($key, $asc, $sort_flags);
usort($array, $cmp);
//return $array;
}
function _usortByArrayKey($key, $asc=SORT_ASC, array $sort_flags, array $a, array $b){
if(!is_array($key)) { //just one key and sort direction
if(!isset($a[$key]) || !isset($b[$key])) {
throw new Exception('attempting to sort on non-existent keys');
}
if($a[$key] == $b[$key]) {
return 0;
}
return ($asc==SORT_ASC xor $a[$key] < $b[$key]) ? 1 : -1;
} else { //using multiple keys for sort and sub-sort
foreach($key as $sub_key => $sub_asc) {
//array can come as 'sort_key'=>SORT_ASC|SORT_DESC or just 'sort_key', so need to detect which
if(!in_array($sub_asc, $sort_flags))
{
$sub_key = $sub_asc; $sub_asc = $asc;
}
//just like above, except 'continue' in place of return 0
if(!isset($a[$sub_key]) || !isset($b[$sub_key])) {
throw new Exception('attempting to sort on non-existent keys');
}
if($a[$sub_key] == $b[$sub_key]) {
continue;
}
return ($sub_asc==SORT_ASC xor $a[$sub_key] < $b[$sub_key]) ? 1 : -1;
}
return 0;
}
}
しかし、私はまだエラーが発生しています
エラー :
Message: Argument 4 passed to ConnectList::_usortByArrayKey() must be an array, none given, called in C:\xampp\htdocs\receiverlist\application\models\connect.php on line 321 and defined
Uncaught exception 'Exception' with message 'attempting to sort on non-existent keys'