[英]Sort an array based on two other arrays
I have a total of three arrays 我总共有三个阵列
Array 1 数组1
array1 = Array(
[0] => Array(
[name] => John
[city] => San Francisco
[state] => CA
)
[1] => Array(
[name] => Smith
[city] => Atlanta
[state] => GA
)
[2] => Array(
[name] => Peter
[city] => New York
[state] => NY
)
[3] => Array(
[name] => Mary
[city] => San Jose
[state] => CA
)
)
Array 2 (sorted on average age) 数组2(按平均年龄排序)
array2 = Array(
[0] => CA
[1] => NY
[2] => GA
)
Array 3 (sorted based on population) 数组3(根据人口排序)
array3 = Array(
[0] => New York
[1] => San Francisco
[2] => Atlanta
[3] => San Jose
)
How do I sort array 1 based on array 2 first and then by array 3. I want the following output: 如何首先基于数组2排序数组1,然后按数组3排序。我想要以下输出:
Sorted Array 1 排序数组1
Array
(
[0] => Array
(
[name] => John
[city] => San Francisco
[state] => CA
)
[1] => Array
(
[name] => Mary
[city] => San Jose
[state] => CA
)
[2] => Array
(
[name] => Peter
[city] => New York
[state] => NY
)
[3] => Array
(
[name] => Smith
[city] => Atlanta
[state] => GA
)
)
I tried using array_multisort
for achieving this, but it's not working, eg 我尝试使用
array_multisort
来实现这一点,但它不起作用,例如
array_multisort($array2,$array3,$array1)
Any help is highly appreciated. 任何帮助都非常感谢。
Here's one way to sort $array1
by $array2
and $array3
to produce the results you want: 这是通过
$array2
和$array3
对$array1
进行排序以产生所需结果的一种方法:
<?php
$array1 = array(
array('name' => 'John', 'city' => 'San Francisco', 'state' => 'CA'),
array('name' => 'Smith', 'city' => 'Atlanta', 'state' => 'GA'),
array('name' => 'Peter', 'city' => 'New York', 'state' => 'NY'),
array('name' => 'Mary', 'city' => 'San Jose', 'state' => 'CA'),
);
$array2 = array(
'CA',
'NY',
'GA'
);
$array3 = array(
'New York',
'San Francisco',
'Atlanta',
'San Jose'
);
usort(
$array1,
function ($element1, $element2) use ($array2, $array3) {
$state_sort_order = array_flip($array2);
$element1_state_sort_order = $state_sort_order[$element1['state']];
$element2_state_sort_order = $state_sort_order[$element2['state']];
$state_sort_result = $element1_state_sort_order - $element2_state_sort_order;
if ($state_sort_result === 0) {
$city_sort_order = array_flip($array3);
$element1_city_sort_order = $city_sort_order[$element1['city']];
$element2_city_sort_order = $city_sort_order[$element2['city']];
return $element1_city_sort_order - $element2_city_sort_order;
} else {
return $state_sort_result;
}
}
);
print_r($array1);
Again, usort seemed to be the choice with the most flexibility. 同样, usort似乎是最灵活的选择。 There might be a way to figure out how to use array_multisort , but I couldn't tell based on the data you've given.
可能有办法弄清楚如何使用array_multisort ,但我无法根据你给出的数据来判断。
I use a function closure, capturing $array2
and $array3
from the outer scope to do the sorting. 我使用函数闭包,从外部作用域捕获
$array2
和$array3
来进行排序。 It defines a comparison function for usort
to apply to elements of $array1
to determine sort order. 它为
usort
定义了一个比较函数,以应用于$array1
元素来确定排序顺序。 In this function, $element1
and $element2
each receive the value of elements from $array1
to process for comparison, eg 'Smith,Atlanta,GA'
, or 'Mary,San Jose,CA'
. 在此函数中,
$element1
和$element2
各自从$array1
接收元素的值以进行比较,例如'Smith,Atlanta,GA'
或'Mary,San Jose,CA'
。
For each element, I extract the city and state portions of the elements. 对于每个元素,我提取元素的城市和州部分。 If you flip
$array2
and $array3
, they become lookup tables for sort order when you index them by $city
and $state
, respectively. 如果你翻转
$array2
和$array3
,当你按$city
和$state
索引它们时,它们就会成为排序顺序的查找表。
The rest is just returning the difference between city sort orders if the state sort orders are equal, else just return the state sort orders. 如果状态排序顺序相等,其余的只是返回城市排序顺序之间的差异,否则只返回状态排序顺序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.