1
나는 사용자 정의 순서로 배열에있는 디렉토리 목록을 정렬하려고합니다.usort를 사용하여 PHP에서 배열을 사용자 지정 정렬
나는 usort를 사용해 보았지만 원하는 결과를 얻지 못했습니다.
또한 여기 usort에서 사용되는 $ a와 $ b에 대해 설명하고 싶습니다.
$dir = '/tmp';
$dir_list=scan_dir($dir);
//now this $dir_list array for the sake of this example contains the following elements
$dir_list=array('dir1','dir2','newdir2','string1');
$order=array('newdir2','dir1','string1','dir2');
//all the values in both the arrays are strings.
내가 여기에 다음과 같은 해결책을 찾았지만 $ A, $ B와 키워드 '사용'의 사용이
usort($dir_list, function ($a, $b) use ($order)
{
$pos_a = array_search($a['id'], $order);
$pos_b = array_search($b['id'], $order);
return $pos_a - $pos_b;
});
var_dump($dir_list);
'usort' 설명을 읽으셨습니까? (http://www.php.net/manual/en/function.usort.php) – hindmost