Jump to content
  • Hello visitors, welcome to the Hacker World Forum!

    Red Team 1949  (formerly CHT Attack and Defense Team) In this rapidly changing Internet era, we maintain our original intention and create the best community to jointly exchange network technologies. You can obtain hacker attack and defense skills and knowledge in the forum, or you can join our Telegram communication group to discuss and communicate in real time. All kinds of advertisements are prohibited in the forum. Please register as a registered user to check our usage and privacy policy. Thank you for your cooperation.

    TheHackerWorld Official

PHP二维数组排序(简单易懂版)

 Share


HACK1949

Recommended Posts

1.先定义一个数组  

$data[] = array('volume' => 67, 'asd'=>'b','edition' => 2);
$data[] = array('volume' => 86,'cc'=>'b', 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6,'test'=>'b');
$data[] = array('eq'=>'b','volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 3,'jr'=>'b');
$data[] = array('wt'=>'b','volume' => 67, 'edition' => 7);

 

2.获取要排序的字段

$edit = array_column($data,'edition');

 

3.先进行列排序,然后让$data数组按该字段来排序。

array_multisort($edit,SORT_DESC,$data);

 

4.此时$data就按 edition 字段倒序的方式来排序了!!

 

5.如果还有其他排序条件,则往函数后添加,最后再填写$data。 和mysql order相似,参数的字段越前,优先级越高。

$vol = array_column($data,'volume');
$edit = array_column($data,'edition');
array_multisort($vol,SORT_ASC,$edit,SORT_DESC,$data);

 

6.此时是先按 volume升序,然后值相同时就按 edition降序 来排序了!

2413497-20220720095122688-1456056865.png

 

Link to post
Link to comment
Share on other sites

 Share

discussion group

discussion group

    You don't have permission to chat.
    • Recently Browsing   0 members

      • No registered users viewing this page.
    ×
    ×
    • Create New...