PHP 兩個多維陣列根據某個鍵的值進行組合排序的幾種思路

來源:互聯網
上載者:User

標籤:

需求:

有兩個多維陣列

$arr = [    [‘name‘=>‘dee‘,‘age‘=>28],    [‘name‘=>‘emperor‘, ‘age‘=>30],    [‘name‘=>‘Lee‘, ‘age‘=>32],    [‘name‘=>‘Arshavin‘, ‘age‘=>33]];$arr2 = [    [‘name‘=>‘Ozil‘, ‘age‘=>27],    [‘name‘=>‘CR7‘, ‘age‘=>29],    [‘name‘=>‘Doctor‘, ‘age‘=>33],    [‘name‘=>‘John‘, ‘age‘=>35]];

需要將兩個數組合并,並且根據 ‘age‘ 從小到大進行排序,也就是最終的數組是:

Array(    [0] => Array([name] => Ozil [age] => 27)    [1] => Array([name] => dee [age] => 28)    [2] => Array([name] => CR7 [age] => 29)    [3] => Array([name] => emperor [age] => 30)    [4] => Array([name] => Lee [age] => 32)    [5] => Array([name] => Arshavin [age] => 33)    [6] => Array([name] => Doctor [age] => 33)    [7] => Array([name] => John [age] => 35)
[8] => Array([name] => Totti [age] => 40)
)

 

 

有幾種思路:

1. 利用 sort() 函數對年齡進行排序

<?php$arr = [[‘name‘=>‘dee‘,‘age‘=>28],[‘name‘=>‘emperor‘, ‘age‘=>30],[‘name‘=>‘Lee‘, ‘age‘=>32],[‘name‘=>‘Arshavin‘, ‘age‘=>33],[‘name‘=>‘Totti‘, ‘age‘=>40]];$arr2 = [[‘name‘=>‘Ozil‘, ‘age‘=>27],[‘name‘=>‘CR7‘, ‘age‘=>29],[‘name‘=>‘Doctor‘, ‘age‘=>33],[‘name‘=>‘John‘, ‘age‘=>35]];echo ‘<pre>‘;$tmp = array_merge($arr, $arr2);$age = [];foreach($tmp as $key => $val) {$age[] = $val[‘age‘];}sort($age);$new = [];foreach($age as $key => $val) {foreach($tmp as $k => $v) {if($val == $v[‘age‘]) {$new[$key] = $v;unset($tmp[$k]);}}}print_r($new);

  

2. 使用冒泡排序代替 sort() 函數(冒泡排序比 sort() 函數更高效)

<?php$arr = [[‘name‘=>‘dee‘,‘age‘=>28],[‘name‘=>‘emperor‘, ‘age‘=>30],[‘name‘=>‘Lee‘, ‘age‘=>32],[‘name‘=>‘Arshavin‘, ‘age‘=>33],[‘name‘=>‘Totti‘, ‘age‘=>40]];$arr2 = [[‘name‘=>‘Ozil‘, ‘age‘=>27],[‘name‘=>‘CR7‘, ‘age‘=>29],[‘name‘=>‘Doctor‘, ‘age‘=>33],[‘name‘=>‘John‘, ‘age‘=>35]];echo ‘<pre>‘;$tmp = array_merge($arr, $arr2);$age = [];foreach($tmp as $key => $val) {$age[] = $val[‘age‘];}//冒泡排序for($i = 0; $i < count($age) - 1; $i++) {for($j = 0; $j < count($age) - $i - 1; $j++) {if($age[$j] > $age[$j+1]) {$t = $age[$j];$age[$j] = $age[$j+1];$age[$j+1] = $t;}}}$new = [];foreach($age as $key => $val) {foreach($tmp as $k => $v) {if($val == $v[‘age‘]) {$new[$key] = $v;unset($tmp[$k]); }}}print_r($new);

  

3. 稍複雜

<?php$arr = [[‘name‘=>‘dee‘,‘age‘=>28],[‘name‘=>‘emperor‘, ‘age‘=>30],[‘name‘=>‘Lee‘, ‘age‘=>32],    [‘name‘=>‘Arshavin‘, ‘age‘=>33],[‘name‘=>‘Totti‘, ‘age‘=>40]];$arr2 = [[‘name‘=>‘Ozil‘, ‘age‘=>27],[‘name‘=>‘CR7‘, ‘age‘=>29],[‘name‘=>‘Doctor‘, ‘age‘=>33],[‘name‘=>‘John‘, ‘age‘=>35]];echo ‘<pre>‘;$tmp = [];foreach($arr as $key => $val) {foreach($arr2 as $k => $v) {if($key == $k && 0 == $key) {if($v[‘age‘] < $val[‘age‘]) {$tmp[] = $v;unset($arr2[0]);} elseif ($v[‘age‘] > $val[‘age‘]) {$tmp[] = $val;                unset($arr[0]);} else {                $tmp[] = $val;                $tmp[] = $v;                unset($arr[0]);                unset($arr2[0]);            }} else {if($v[‘age‘] < $val[‘age‘]) {$tmp[] = $v;unset($arr2[$k]);                if(empty($arr2)) {                    $tmp = array_merge($tmp, $arr);                }} elseif($v[‘age‘] > $val[‘age‘]) {$tmp[] = $val;                unset($arr[$key]);                if(empty($arr)) {                    $tmp = array_merge($tmp, $arr2);                }                break;} else {                $tmp[] = $val;                $tmp[] = $v;                unset($arr2[$k]);                unset($arr[$key]);                if(empty($arr)) {                    $tmp = array_merge($tmp, $arr2);                }                break;            }}}}print_r($tmp);

  

4. 把年齡作為索引,使用 ksort() 函數進行排序。缺點:如果遇到了相同的年齡,則只能保留一組索引值

<?php$arr = [[‘name‘=>‘dee‘,‘age‘=>28],[‘name‘=>‘emperor‘, ‘age‘=>30],[‘name‘=>‘Lee‘, ‘age‘=>32],[‘name‘=>‘Arshavin‘, ‘age‘=>33],[‘name‘=>‘Totti‘, ‘age‘=>40]];$arr2 = [[‘name‘=>‘Ozil‘, ‘age‘=>27],[‘name‘=>‘CR7‘, ‘age‘=>29],[‘name‘=>‘Doctor‘, ‘age‘=>33],[‘name‘=>‘John‘, ‘age‘=>35]];echo ‘<pre>‘;foreach($arr as $key => $val) {$arr[$val[‘age‘]] = $val;unset($arr[$key]);}foreach($arr2 as $key => $val) {$arr2[$val[‘age‘]] = $val;unset($arr2[$key]);}$new = $arr + $arr2; //遇到相同的索引時,只會保留第一個數組中重複的索引值ksort($new);$new = array_values($new);print_r($new);/*說明:結果集中沒有 $arr2 中索引為"Doctor"的索引值Array(    [0] => Array        (            [name] => Ozil            [age] => 27        )    [1] => Array        (            [name] => dee            [age] => 28        )    [2] => Array        (            [name] => CR7            [age] => 29        )    [3] => Array        (            [name] => emperor            [age] => 30        )    [4] => Array        (            [name] => Lee            [age] => 32        )    [5] => Array        (            [name] => Arshavin            [age] => 33        )    [6] => Array        (            [name] => John            [age] => 35        )    [7] => Array        (            [name] => Totti            [age] => 40        ))

  

 

PHP 兩個多維陣列根據某個鍵的值進行組合排序的幾種思路

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.