duanqiang9212 2013-11-29 13:06
浏览 218
已采纳

如何按字母顺序对数组进行排序,然后将该顺序分配给另一个数组?

I got 2 Arrays. First one looks like this:

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"
  ...
}

and the other one looks like this:

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"
  ...
}

As you can see it is possible to sort the first array but not the second because of the single digits. With asort($first_array) I got the correct order for that array. But I need the same order for my second array. How could I do this? array_multisort($first_array, $asorted_second_array); didnt work so far. Any tips maybe?

Regards

  • 写回答

2条回答 默认 最新

  • dongshan8194 2013-11-29 13:59
    关注

    Since you're going to set order in second array exactly as it will be in first array, I assume both arrays has equal count of elements. Therefore, you can use:

    //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] 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题