I have an array which need to sort using a key value of "odbyx"
我有一个数组,需要使用键值“odbyx”进行排序
Here is the array var_dump
这是数组var_dump
array(12) {
["id"]=> array(7) {
[0]=> string(1) "8"
[1]=> string(1) "7"
[2]=> string(1) "3"
[3]=> string(1) "6"
[4]=> string(1) "5"
[5]=> string(1) "2"
[6]=> string(1) "1"
}
["subject"]=> array(7) {
[0]=> string(14) "ticke tick sbj"
[1]=> string(13) "new tick test"
[2]=> string(15) "fdsfdsfdsfdsfds"
[3]=> string(12) "test subject"
[4]=> string(4) "test"
[5]=> string(4) "test"
[6]=> string(12) "test subject"
}
["msg"]=> array(7) {
[0]=> string(9) "test+tick"
[1]=> string(4) "test"
[2]=> string(9) "dfdsfdsfd"
[3]=> string(12) "test+tcikets"
[4]=> string(4) "test"
[5]=> string(12) "test+message"
[6]=> string(7) "tssssss"
}
["department"]=> array(7) {
[0]=> string(10) "Technician"
[1]=> string(5) "Admin"
[2]=> string(5) "Admin"
[3]=> string(10) "Technician"
[4]=> string(10) "Technician"
[5]=> string(5) "Admin"
[6]=> string(5) "Admin"
}
["priorety"]=> array(7) {
[0]=> string(3) "Low"
[1]=> string(6) "Normal"
[2]=> string(3) "Low"
[3]=> string(3) "Low"
[4]=> string(4) "High"
[5]=> string(6) "Normal"
[6]=> string(3) "Low"
}
["status"]=> array(7) {
[0]=> string(4) "open"
[1]=> string(8) "answered"
[2]=> string(8) "answered"
[3]=> string(4) "open"
[4]=> string(4) "open"
[5]=> string(4) "open"
[6]=> string(6) "closed"
}
["dateAded"]=> array(7) {
[0]=> string(19) "2017-10-01 12:34:56"
[1]=> string(19) "2017-09-27 13:41:09"
[2]=> string(19) "2017-09-17 13:53:04"
[3]=> string(19) "2017-09-25 15:00:48"
[4]=> string(19) "2017-09-23 10:41:24"
[5]=> string(19) "2017-09-17 13:31:56"
[6]=> string(19) "2017-09-17 12:37:22"
}
["dateClosed"]=> array(7) {
[0]=> string(19) "0000-00-00 00:00:00"
[1]=> string(19) "0000-00-00 00:00:00"
[2]=> string(19) "0000-00-00 00:00:00"
[3]=> string(19) "0000-00-00 00:00:00"
[4]=> string(19) "0000-00-00 00:00:00"
[5]=> string(19) "2017-09-30 13:41:09"
[6]=> string(19) "2017-09-17 13:40:53"
}
["dateActivity"]=> array(7) {
[0]=> string(19) "2017-10-01 12:34:56"
[1]=> string(19) "2017-10-01 07:49:20"
[2]=> string(19) "2017-09-26 10:35:36"
[3]=> string(19) "2017-09-25 15:00:48"
[4]=> string(19) "2017-09-23 10:41:24"
[5]=> string(19) "2017-09-17 13:41:21"
[6]=> string(19) "2017-09-17 13:40:53"
}
["userId"]=> array(7) {
[0]=> string(1) "4"
[1]=> string(1) "4"
[2]=> string(1) "3"
[3]=> string(1) "4"
[4]=> string(1) "7"
[5]=> string(1) "3"
[6]=> string(1) "2"
}
["res"]=> string(4) "true"
["odbyx"]=> array(7) {
[0]=> int(1)
[1]=> int(2)
[2]=> int(3)
[3]=> int(2)
[4]=> int(3)
[5]=> int(3)
[6]=> int(3)
}
}
Logic behind the array
阵列背后的逻辑
$array = array
(
"id" => $id,
"subject" => $subject,
"msg" => $msg,
"department" => $department,
"priorety" => $priorety,
"status" => $status,
"dateAded" => $dateAded,
"dateClosed" => $dateClosed,
"dateActivity" => $dateActivity,
"userId" => $userId,
"res" => $res,
"odbyx"=>$odbyx
);
I tried sort using below code but it seems not working
我尝试使用下面的代码排序,但它似乎无法正常工作
array_multisort(array_column($array, 'odbyx'), SORT_DESC, $array);
odbyx field hold a priority values (1,2,3).
odbyx字段保持优先级值(1,2,3)。
i need to sort main array using that values, so i can display top priority fields at the beginning soo on a table
我需要使用该值对主数组进行排序,因此我可以在表的开头soo显示最高优先级字段
I want to sort the $array using "odbyx" descending order
我想使用“odbyx”降序对$数组进行排序
Example Output of id array, the other arrays also need to sort at once.
示例id数组的输出,其他数组也需要一次排序。
["id"]=> array(7) { [0]=> string(1) "6" [1]=> string(1) "5" [2]=> string(1) "4" [3]=> string(1) "3" [4]=> string(1) "7" [5]=> string(1) "6" [6]=> string(1) "8" }
1 个解决方案
#1
1
proper use of usort
See also: Sort Multi-dimensional Array by Value
另请参阅:按值排序多维数组
your example is invalid
Expected first entry in result: 0 => '6'
预期首次输入结果:0 =>'6'
Possible interpretation odbyx-index == id-index
Should indices in odbyx
match to indices in id
?
odbyx中的索引应该与id中的索引匹配吗?
i.e.: $array['id'][3]
should be sorted by $array['odbyx'][3]
即:$ array ['id'] [3]应按$ array ['odbyx']排序[3]
If this is the case, then your provided code should either yield 0 => '8'
for the first index (odbyx 1 higher-priority than 3), or 0 => '1'
(3 is higher).
如果是这种情况,那么您提供的代码应该为第一个索引(odbyx 1优先级高于3)产生0 =>'8',或者0 =>'1'(3更高)。
Possible interpretation odbyx-index == id-value
Should the index in odbyx
match to the id values in id
?
odbyx中的索引是否应该与id中的id值匹配?
i.e.: The value of $array['odbyx'][1]
determines the sort for $array['id'][6] = '1'
即:$ array ['odbyx'] [1]的值确定$ array ['id'] [6] ='1'的排序
In this case, the result should be 0 => '2'
在这种情况下,结果应为0 =>'2'
None of these possible interpretations even match the very first result in your example. The lesson here is specification, i.e.: carefully define and describe the specific conditions required to solve your problem, on * or anywhere else.
这些可能的解释都不会与您示例中的第一个结果相匹配。这里的教训是规范,即:仔细定义和描述解决问题所需的特定条件,在*或其他任何地方。
Here's a place to start
Since the problem you are asking to be solved is complex, poorly defined, would require a significant amount of coding and testing, and has significant performance implications, I'll leave you with this tidbit of a solution to one of the impossible interpretations above. Good luck.
由于您要求解决的问题很复杂,定义不明确,需要大量的编码和测试,并且具有重要的性能影响,因此我将为您提供上述不可能解释之一的解决方案。祝好运。
Class SimpleSorter
{
private $orderBy;
private $sortMe;
public static function sortByIndexedOrderField(array $sortMe, array $byMe)
{
$sorter = new self($sortMe);
return $sorter->applyIndexedOrder($byMe);
}
public function __construct(array $sortMe)
{
$this->sortMe = $sortMe;
}
public function applyIndexedOrder(array $byMe): array
{
$this->orderBy = $byMe;
$keys = array_keys($this->sortMe);
// sort, first by odbyx, then by value
usort($keys, function($a,$b){
$odbyx = 0;
if (array_key_exists($a, $this->orderBy) && array_key_exists($b, $this->orderBy)) {
$odbyx = $this->orderBy[$b] <=> $this->orderBy[$a];
}
if (0 !== $odbyx) {
return $odbyx;
}
return $this->sortMe[$a] <=> $this->sortMe[$b];
});
// reorder by new key order
$result = [];
foreach($keys as $key) {
$result[$key] = $this->sortMe[$key];
}
return $result;
}
}
$array = [];
$array["id"] = [
0 => '8',
1 => '7',
2 => '3',
3 => '6',
4 => '5',
5 => '2',
6 => '1',
];
$array["odbyx"] = [
0 => 1,
1 => 2,
2 => 3,
3 => 2,
4 => 3,
5 => 3,
6 => 3,
];
$idsSorted = SimpleSorter::sortByIndexedOrderField($array["id"], $array["odbyx"]);
print_r($idsSorted);
#1
1
proper use of usort
See also: Sort Multi-dimensional Array by Value
另请参阅:按值排序多维数组
your example is invalid
Expected first entry in result: 0 => '6'
预期首次输入结果:0 =>'6'
Possible interpretation odbyx-index == id-index
Should indices in odbyx
match to indices in id
?
odbyx中的索引应该与id中的索引匹配吗?
i.e.: $array['id'][3]
should be sorted by $array['odbyx'][3]
即:$ array ['id'] [3]应按$ array ['odbyx']排序[3]
If this is the case, then your provided code should either yield 0 => '8'
for the first index (odbyx 1 higher-priority than 3), or 0 => '1'
(3 is higher).
如果是这种情况,那么您提供的代码应该为第一个索引(odbyx 1优先级高于3)产生0 =>'8',或者0 =>'1'(3更高)。
Possible interpretation odbyx-index == id-value
Should the index in odbyx
match to the id values in id
?
odbyx中的索引是否应该与id中的id值匹配?
i.e.: The value of $array['odbyx'][1]
determines the sort for $array['id'][6] = '1'
即:$ array ['odbyx'] [1]的值确定$ array ['id'] [6] ='1'的排序
In this case, the result should be 0 => '2'
在这种情况下,结果应为0 =>'2'
None of these possible interpretations even match the very first result in your example. The lesson here is specification, i.e.: carefully define and describe the specific conditions required to solve your problem, on * or anywhere else.
这些可能的解释都不会与您示例中的第一个结果相匹配。这里的教训是规范,即:仔细定义和描述解决问题所需的特定条件,在*或其他任何地方。
Here's a place to start
Since the problem you are asking to be solved is complex, poorly defined, would require a significant amount of coding and testing, and has significant performance implications, I'll leave you with this tidbit of a solution to one of the impossible interpretations above. Good luck.
由于您要求解决的问题很复杂,定义不明确,需要大量的编码和测试,并且具有重要的性能影响,因此我将为您提供上述不可能解释之一的解决方案。祝好运。
Class SimpleSorter
{
private $orderBy;
private $sortMe;
public static function sortByIndexedOrderField(array $sortMe, array $byMe)
{
$sorter = new self($sortMe);
return $sorter->applyIndexedOrder($byMe);
}
public function __construct(array $sortMe)
{
$this->sortMe = $sortMe;
}
public function applyIndexedOrder(array $byMe): array
{
$this->orderBy = $byMe;
$keys = array_keys($this->sortMe);
// sort, first by odbyx, then by value
usort($keys, function($a,$b){
$odbyx = 0;
if (array_key_exists($a, $this->orderBy) && array_key_exists($b, $this->orderBy)) {
$odbyx = $this->orderBy[$b] <=> $this->orderBy[$a];
}
if (0 !== $odbyx) {
return $odbyx;
}
return $this->sortMe[$a] <=> $this->sortMe[$b];
});
// reorder by new key order
$result = [];
foreach($keys as $key) {
$result[$key] = $this->sortMe[$key];
}
return $result;
}
}
$array = [];
$array["id"] = [
0 => '8',
1 => '7',
2 => '3',
3 => '6',
4 => '5',
5 => '2',
6 => '1',
];
$array["odbyx"] = [
0 => 1,
1 => 2,
2 => 3,
3 => 2,
4 => 3,
5 => 3,
6 => 3,
];
$idsSorted = SimpleSorter::sortByIndexedOrderField($array["id"], $array["odbyx"]);
print_r($idsSorted);