问题描述
我有一个数组,需要按另一个数组的形式对2个字段的邮政编码进行排序并批准
我可以按邮政编码对其进行排序,但无法在批准的字段中进行排序,例如
我需要按60007,60001,60003,60002排序(按照类似排序的顺序)
$ sortLike = array(60007,60001,60003,60002);$ array1 =数组(数组('ID'=> 138,'zip_code'=> 60007,'approved'=> 1),array('ID'=> 103,'zip_code'=> 60007,'approved'=> 0),array('ID'=> 114,'zip_code'=> 60007,'approved'=> 1),array('ID'=> 105,'zip_code'=> 60003,'approved'=> 0),array('ID'=> 124,'zip_code'=> 60002,'approved'=> 0))
因此,60007和经过批准的版本应该比带有0(未批准)的60007和所有60001批准的版本都要比未经批准的所有60001优先(依 $ sortlike
依此类推),这里是完整的php代码
<?php$ array1 =数组(数组('ID'=> 138,'zip_code'=> 60007,'approved'=> 1),array('ID'=> 103,'zip_code'=> 60007,'approved'=> 0),array('ID'=> 114,'zip_code'=> 60007,'approved'=> 1),array('ID'=> 105,'zip_code'=> 60003,'approved'=> 0),array('ID'=> 124,'zip_code'=> 60002,'approved'=> 0),array('ID'=> 104,'zip_code'=> 60002,'approved'=> 1),array('ID'=> 106,'zip_code'=> 60001,'approved'=> 0),array('ID'=> 188,'zip_code'=> 60022,'approved'=> 0),array('ID'=> 184,'zip_code'=> 60022,'approved'=> 1),);函数sort_results($ a,$ b){$ sortLike = array(60007,60001,60003,60002);如果(!in_array($ a ['zip_code'],$ sortLike)){返回1;}//将未知值推入数组的末尾返回array_search($ a ['zip_code'],$ sortLike)>array_search($ b ['zip_code'],$ sortLike);}usort($ array1,'sort_results');回声< pre>";print_r($ array1);回声</pre>";
我想您是在问如何也可以按'approved'
进行排序,因为您的代码当前缺少该信息./p>
此问题的解决方案是定义一个检查输入数组 $ array1
的键的顺序.
因此,您首先需要检查 zip_code
.如果得到结果!= 0,则可以立即返回.但是,如果结果为== 0(邮政编码相等),则需要检查第二个密钥(本例中为'approved'
).
$ array1 =数组(数组('ID'=> 138,'zip_code'=> 60007,'approved'=> 1),array('ID'=> 103,'zip_code'=> 60007,'approved'=> 0),array('ID'=> 114,'zip_code'=> 60007,'approved'=> 1),array('ID'=> 105,'zip_code'=> 60003,'approved'=> 0),array('ID'=> 124,'zip_code'=> 60002,'approved'=> 0),array('ID'=> 104,'zip_code'=> 60002,'approved'=> 1),array('ID'=> 106,'zip_code'=> 60001,'approved'=> 0),array('ID'=> 188,'zip_code'=> 60022,'approved'=> 0),array('ID'=> 184,'zip_code'=> 60022,'approved'=> 1),);函数sort_results($ a,$ b){$ sortLike = array(60007,60001,60003,60002);如果(!in_array($ a ['zip_code'],$ sortLike)){返回1;}//将未知值推入数组的末尾$ zip_res = array_search($ a ['zip_code'],$ sortLike)-array_search($ b ['zip_code'],$ sortLike);//检查邮政编码顺序if($ zip_res == 0){//如果邮政编码相等$ zip_res = $ b ['approved']-$ a ['approved'];//按批准"键排序}返回$ zip_res;}
//编辑
您可以使用lamda函数将 $ sortLike
移出该函数(需要PHP> = 5.3):
$ sortLike = array(60007,60001,60003,60002);$ sort_results =函数($ a,$ b)use($ sortLike){//创建带有"use"关键字的lambda函数如果(!in_array($ a ['zip_code'],$ sortLike)){返回1;}//将未知值推入数组的末尾$ zip_res = array_search($ a ['zip_code'],$ sortLike)-array_search($ b ['zip_code'],$ sortLike);if($ zip_res == 0){$ zip_res = $ b ['approved']-$ a ['approved'];}返回$ zip_res;};usort($ array1,$ sort_results);//在中传递lambda函数回声< pre>";print_r($ array1);回声</pre>";死();
I have array that I need to sort by another array form 2 fields zip code and approve
I am able to sort it by zip code but unable to do it with approved field so for eg
I need to sort by 60007,60001,60003,60002 (as per sortlike order)all zip code from 60007 and approved should come first so
$sortLike=array(60007,60001,60003,60002);
$array1= array(
array ('ID' => 138,'zip_code' => 60007,'approved' => 1),
array('ID' => 103,'zip_code' => 60007,'approved' => 0),
array('ID' => 114,'zip_code' => 60007,'approved' => 1),
array('ID' => 105,'zip_code' => 60003,'approved' => 0),
array('ID' => 124,'zip_code' => 60002,'approved' => 0)
)
so 60007 and aproved should come first than 60007 with 0(unapproved) and than all 60001 approved than all 60001 unapproved(and so on as per $sortlike
) here is complete php code
<?php
$array1= array(
array ('ID' => 138,'zip_code' => 60007,'approved' => 1),
array('ID' => 103,'zip_code' => 60007,'approved' => 0),
array('ID' => 114,'zip_code' => 60007,'approved' => 1),
array('ID' => 105,'zip_code' => 60003,'approved' => 0),
array('ID' => 124,'zip_code' => 60002,'approved' => 0),
array('ID' => 104,'zip_code' => 60002,'approved' => 1),
array('ID' => 106,'zip_code' => 60001,'approved' => 0),
array('ID' => 188,'zip_code' => 60022,'approved' => 0),
array('ID' => 184,'zip_code' => 60022,'approved' => 1),
);
function sort_results ($a, $b) {
$sortLike=array(60007,60001,60003,60002);
if (!in_array ($a['zip_code'], $sortLike)) { return 1; } // Push unknown values at the end of the array
return array_search($a['zip_code'], $sortLike) > array_search($b['zip_code'], $sortLike);
}
usort ($array1, 'sort_results');
echo "<pre>";
print_r($array1);
echo "</pre>";
I guess you're asking how you can also sort by 'approved'
, because that is currently missing in your code.
The solution to this question is to define an order in which the keys of your input array $array1
are checked.
So you need to check zip_code
at first. If you get a result != 0, you can return immediately. But if the result is == 0 (the zip codes are equal) you need to check the second key ('approved'
in your case).
$array1= array(
array ('ID' => 138,'zip_code' => 60007,'approved' => 1),
array('ID' => 103,'zip_code' => 60007,'approved' => 0),
array('ID' => 114,'zip_code' => 60007,'approved' => 1),
array('ID' => 105,'zip_code' => 60003,'approved' => 0),
array('ID' => 124,'zip_code' => 60002,'approved' => 0),
array('ID' => 104,'zip_code' => 60002,'approved' => 1),
array('ID' => 106,'zip_code' => 60001,'approved' => 0),
array('ID' => 188,'zip_code' => 60022,'approved' => 0),
array('ID' => 184,'zip_code' => 60022,'approved' => 1),
);
function sort_results ($a, $b) {
$sortLike=array(60007,60001,60003,60002);
if (!in_array ($a['zip_code'], $sortLike)) { return 1; } // Push unknown values at the end of the array
$zip_res = array_search($a['zip_code'], $sortLike) - array_search($b['zip_code'], $sortLike); // check zip_code order
if($zip_res == 0){ // if the zip_codes are equal
$zip_res = $b['approved'] - $a['approved']; // sort by the 'approved' key
}
return $zip_res;
}
//edit
You can use a lamda function to move $sortLike
out of the function like this (requires PHP >= 5.3):
$sortLike=array(60007,60001,60003,60002);
$sort_results = function ($a, $b) use ($sortLike){ // create lambda function with "use" keyword
if (!in_array ($a['zip_code'], $sortLike)) { return 1; } // Push unknown values at the end of the array
$zip_res = array_search($a['zip_code'], $sortLike) - array_search($b['zip_code'], $sortLike);
if($zip_res == 0){
$zip_res = $b['approved'] - $a['approved'];
}
return $zip_res;
};
usort ($array1,$sort_results); // pass lambda function in
echo "<pre>";
print_r($array1);
echo "</pre>";
die();
这篇关于基于另一个数组的usort multisort数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!