php 數組這樣的排序怎麼做

來源:互聯網
上載者:User
$a = array(    '1' => 'one',    '2' => 'two',    '3' => 'three',    '4' => 'four',    '5' => 'five',    ....);$b = array(2,5,4,1,3,.....); // 數組a的key順序$c = array(    '2' => 'two',    '5' => 'five',    '4' => 'four',    '1' => 'one',    '3' => 'three',    ....);

如何將數組a按照數組b值中key的順序重新排序得到數組c?(可以不用迴圈嗎?)

回複內容:

$a = array(    '1' => 'one',    '2' => 'two',    '3' => 'three',    '4' => 'four',    '5' => 'five',    ....);$b = array(2,5,4,1,3,.....); // 數組a的key順序$c = array(    '2' => 'two',    '5' => 'five',    '4' => 'four',    '1' => 'one',    '3' => 'three',    ....);

如何將數組a按照數組b值中key的順序重新排序得到數組c?(可以不用迴圈嗎?)

樓上的答案都是正確的 ... 不過不夠帥 ... 我畫蛇添足一下好了 ...

如果原數組的索引值不是數字而是字串的話 ... 這樣的排序可以一行解決 ...

  'foo',    'b'   =>  'bar',    'c'   =>  'baz',];/* the order array ... */$order = [ 'b', 'a', 'c' ];/* sort using single line ... */print_r( array_merge( array_flip( $order ), $src ) );

但是 array_merge() 這個函數有個倒黴特性 ...

當原數組的索引值整形化之後不為零的時候 ... 不論原來的索引值是什麼順序都會被這個函數全部丟棄 ...

所以你的需求沒辦法用這種簡單的方式解決 ... 只能藉助於排序函數 ... 代碼如下 ...

  'foo',    '2'   =>  'bar',    '3'   =>  'baz',];/* the order array ... type is not important ... */$order = [ 1, '3', 0b10 ];/* get order weight ... */$weight = array_flip( $order );/* we do not want to change the source array order huh ..? */$dst = $src;/* 3 lines sorting ... not very difficult ... */uksort( $dst, function( $left, $right ) use ( $weight ) {    /* $left never equals to $right ... so 0 is impossible ... */     return ( $weight[$left] < $weight[$right] ) ? -1 : 1; } );/* show time ... */print_r( $dst );

這種方式雖然寫起來複雜 ... 但時間複雜度要低於樓上的整個遍曆重產生新數組 ...

所以效率更高 ... 並且如果需要更改排序方式的話也更加靈活 ...

恩 ... 就是這樣啦 ...

$a = array(    '1' => 'one',    '2' => 'two',    '3' => 'three',    '4' => 'four',    '5' => 'five',    ....);$b = array(2,5,4,1,3,.....); // 數組a的key順序$c = array();foreach( $b as $d ) $c[$d] = $a[$d];print_r( $c );

$a = array(    '1' => 'one',    '2' => 'two',    '3' => 'three',    '4' => 'four',    '5' => 'five');$b = array('2','5','4','1','3'); // 數組a的key順序$result=array();function sort_key($v){    global $result;    global $a;    $result[$v]=$a[$v];}array_walk($b, 'sort_key');print_r($result);

閑著蛋疼寫了一個...雖然已經時隔將近2年了...

代碼寫得矬,期待優雅代碼。

$a = array(    '1' => 'one',    '2' => 'two',    '3' => 'three',    '4' => 'four',    '5' => 'five',    ...);$b = array(2, 5, 4, 1, 3, ...);$c = array();foreach ($b as $v) {    $c[$v] = $a[$v];}
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.