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

需求:php

有兩個多維數組數組

$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)
)

 

 

有幾種思路:spa

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

<?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() 函數更高效)blog

<?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
        )

)
相關文章
相關標籤/搜索