码迷,mamicode.com
首页 > 编程语言 > 详细

PHP 两个多维数组根据某个键的值进行组合排序的几种思路

时间:2016-04-09 00:12:16      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

需求:

有两个多维数组

$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 两个多维数组根据某个键的值进行组合排序的几种思路

标签:

原文地址:http://www.cnblogs.com/dee0912/p/5370033.html

(0)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!