如何按字母顺序对数组进行排序,然后将该顺序赋予另一个数组


How to sort an array alphabetically and then give that order to another array?

我得到了2个数组。第一个看起来像这样:

array(33) {
  [0]=>
  string(21) "sourcefile,ending_pdf"
  [1]=>
  string(43) "generated_pdf_preview,ending_reviewpdf"
  [2]=>
  string(37) "generated_jpeg_preview_000,ending_jpg"
  [3]=>
  string(37) "generated_jpeg_preview_001,ending_jpg"
  [4]=>
  string(37) "generated_jpeg_preview_010,ending_jpg"
  ...
}

另一个看起来像这样:

array(33) {
  [0]=>
  string(172) "http://my_link/sourcefile.pdf"
  [1]=>
  string(141) "http://my_link/previewpdf"
  [2]=>
  string(149) "http://my_link/my_pdf_file_as_image483568346-0.jpg"
  [3]=>
  string(149) "http://my_link/my_pdf_file_as_image4768746-1.jpg"
  [4]=>
  string(150) "http://my_link/my_pdf_file_as_image6867746-10.jpg"
  [5]=>
  string(150) "http://my_link/my_pdf_file_as_image6867746--11.jpg"
  ...
}

正如您所看到的,可以对第一个数组进行排序,但由于个位数的原因,不能对第二个数组进行分类。使用asort($first_array),我得到了该数组的正确顺序。但我的第二个数组需要相同的顺序。我怎么能这么做?array_multisort($first_array, $asorted_second_array);到目前为止不起作用。有什么建议吗?

问候

由于您将在第二个数组中设置与第一个数组中完全相同的顺序,因此我假设两个数组的元素数相等。因此,您可以使用:

//array that can be (and will be) sorted
$one    = ['rpq','aab', 'rdm', 'llc'];
//array, which order must become same as order of first array
$two    = ['11', '2', '13', '0']; 
$result = array_combine($two, $one);
asort($result);
$result = array_keys($result); //[2, 0, 13, 11] 

关闭,但不将排序数组传递给array_multisort

array_multisort($first_array, $second_array, SORT_ASC);

应该没问题

数组的多重排序会根据排序来改变顺序,所以当你给它传递一个排序的数组时,它不需要改变顺序,因为它已经排序了