dongxin1980 2011-10-14 00:19
浏览 34
已采纳

php,如何排序数组? [关闭]

i have this example:

while (...){
echo "test:'.$a.',- group:'.$m.'
}

this will give me something like:

test:name2 - group:7
test:name3 - group:10
test:name4 - group:3
...

how do i display these results ordered by group, ex:

test:name2 - group:10
test:name3 - group:7
test:name4 - group:3
...

any ideas? i believe i can use something like sort() or array_sort, bu t i don't know exactly

thanks.

edit: $a and m are arrays with the values echoed out.

edit2:

i see that many people don't understand my question, well it is very simple to understand. Also i found my answer and here it is:

$HAy[]=array('a' => $a, 'm' => $m);
foreach($HAy as $c=>$key) {
$sort_num_rec[] = $key['a'];
}
array_multisort($sort_num_rec, SORT_DESC, $HAy);
foreach ($HAy as $ay){
//do something
}

was a bit confuse in the beginning, but i figure it out using array_multisort.

  • 写回答

1条回答 默认 最新

  • doushi7819 2011-10-14 00:43
    关注

    You want to sort multiple arrays based on one of the array's sorting order. use array_multisort();

    http://www.php.net/manual/en/function.array-multisort.php

    array_multisort($m, $a);
    

    this will sort both arrays according to the first arrays sort ordering.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号