按另一个数组中给定的特定顺序对数组值进行排序
Sort array values by specific order given in another array
我有这个顺序正确的数组:
$orderDoc = array("ORT", "TRI", "CONT", "RMI");
这些文件在几个服务器和数据库的不同表中。
在搜索文档结束时,我通常会得到一个具有以下结构但元素处于混乱状态的数组:
//模拟变量:
$FindDoc= array("RMI0000191","ORT0000379","ORT0000391","ORT0000392","ORT0000390","CONT0000274","CONT0000275","RMI0000192","ORT0000391");
输出:
array(10) {
[0]=>
string(10) "RMI0000191"
[1]=>
string(10) "ORT0000379"
[2]=>
string(10) "ORT0000391"
[3]=>
string(10) "ORT0000392"
[4]=>
string(10) "ORT0000390"
[5]=>
string(11) "CONT0000274"
[6]=>
string(11) "CONT0000275"
[7]=>
string(10) "RMI0000192"
[8]=>
string(10) "ORT0000394"
[9]=>
string(10) "TRI0000170"
}
我尝试使用它,但它不起作用。我得到一个错误,因为第二个参数需要是整数:
$FindDoc=asort($FindDoc,$orderDoc );
在调查 none 这个本机函数工作之后:
PHP - Sort Functions For Arrays
In this chapter, we will go through the following PHP array sort functions:
sort() - sort arrays in ascending order
rsort() - sort arrays in descending order
asort() - sort associative arrays in ascending order, according to the value
ksort() - sort associative arrays in ascending order, according to the key
arsort() - sort associative arrays in descending order, according to the value
krsort() - sort associative arrays in descending order, according to the key
https://www.w3schools.com/php/php_arrays_sort.asp
如何获得值的正确顺序?
预期示例,按 $orderDoc ordes 和数字文档排序:
array(10) {
[0]=>
string(10) "ORT0000379"
[1]=>
string(10) "ORT0000390"
[2]=>
string(10) "ORT0000391"
[3]=>
string(10) "ORT0000392"
[4]=>
string(10) "ORT0000394"
[5]=>
string(10) "TRI0000170"
[6]=>
string(11) "CONT0000274"
[7]=>
string(11) "CONT0000275"
[8]=>
string(10) "RMI0000191"
[9]=>
string(10) "RMI0000192"
}
更新我尝试使用自定义函数但出现一些错误:
foreach ($FindDoc as $StructureMember) {
$key = array_search($StructureMember, function ($StructureMember, $orderDoc ) {
return (strpos($StructureMember, $orderDoc ));
});
$result[$key] = $StructureMember;
}
$FindDoc = $result;
输出:
Warning: array_search() expects parameter 2 to be array, object given in C:\xampp\htdocs\class\class.docinfo.php on line 15
循环 orderdoc 并使用 preg_grep 获取所有以相同字符开头的数组项。
然后对返回的数组进行排序,并将它们合并到结果数组中。
$orderDoc = array("ORT", "TRI", "CONT", "RMI");
$FindDoc= array("RMI0000191","ORT0000379","ORT0000391","ORT0000392","ORT0000390","CONT0000274","CONT0000275","RMI0000192","ORT0000391");
$result =[];
foreach($orderDoc as $doc){
$temp = preg_grep("/^". preg_quote($doc) . "/", $FindDoc);
sort($temp);
$result = array_merge($result, $temp);
}
var_dump($result);
$结果:
array(9) {
[0]=>
string(10) "ORT0000379"
[1]=>
string(10) "ORT0000390"
[2]=>
string(10) "ORT0000391"
[3]=>
string(10) "ORT0000391"
[4]=>
string(10) "ORT0000392"
[5]=>
string(11) "CONT0000274"
[6]=>
string(11) "CONT0000275"
[7]=>
string(10) "RMI0000191"
[8]=>
string(10) "RMI0000192"
}
注意:这会保留数组中的重复项。
你不是说应该删除它们,但如果你想删除它们,只需在将 temp 合并到 $result
之前添加 array_unique()
我有这个顺序正确的数组:
$orderDoc = array("ORT", "TRI", "CONT", "RMI");
这些文件在几个服务器和数据库的不同表中。
在搜索文档结束时,我通常会得到一个具有以下结构但元素处于混乱状态的数组:
//模拟变量:
$FindDoc= array("RMI0000191","ORT0000379","ORT0000391","ORT0000392","ORT0000390","CONT0000274","CONT0000275","RMI0000192","ORT0000391");
输出:
array(10) {
[0]=>
string(10) "RMI0000191"
[1]=>
string(10) "ORT0000379"
[2]=>
string(10) "ORT0000391"
[3]=>
string(10) "ORT0000392"
[4]=>
string(10) "ORT0000390"
[5]=>
string(11) "CONT0000274"
[6]=>
string(11) "CONT0000275"
[7]=>
string(10) "RMI0000192"
[8]=>
string(10) "ORT0000394"
[9]=>
string(10) "TRI0000170"
}
我尝试使用它,但它不起作用。我得到一个错误,因为第二个参数需要是整数:
$FindDoc=asort($FindDoc,$orderDoc );
在调查 none 这个本机函数工作之后:
PHP - Sort Functions For Arrays
In this chapter, we will go through the following PHP array sort functions:
sort() - sort arrays in ascending order
rsort() - sort arrays in descending order
asort() - sort associative arrays in ascending order, according to the value
ksort() - sort associative arrays in ascending order, according to the key
arsort() - sort associative arrays in descending order, according to the value
krsort() - sort associative arrays in descending order, according to the key
https://www.w3schools.com/php/php_arrays_sort.asp
如何获得值的正确顺序?
预期示例,按 $orderDoc ordes 和数字文档排序:
array(10) {
[0]=>
string(10) "ORT0000379"
[1]=>
string(10) "ORT0000390"
[2]=>
string(10) "ORT0000391"
[3]=>
string(10) "ORT0000392"
[4]=>
string(10) "ORT0000394"
[5]=>
string(10) "TRI0000170"
[6]=>
string(11) "CONT0000274"
[7]=>
string(11) "CONT0000275"
[8]=>
string(10) "RMI0000191"
[9]=>
string(10) "RMI0000192"
}
更新我尝试使用自定义函数但出现一些错误:
foreach ($FindDoc as $StructureMember) {
$key = array_search($StructureMember, function ($StructureMember, $orderDoc ) {
return (strpos($StructureMember, $orderDoc ));
});
$result[$key] = $StructureMember;
}
$FindDoc = $result;
输出:
Warning: array_search() expects parameter 2 to be array, object given in C:\xampp\htdocs\class\class.docinfo.php on line 15
循环 orderdoc 并使用 preg_grep 获取所有以相同字符开头的数组项。
然后对返回的数组进行排序,并将它们合并到结果数组中。
$orderDoc = array("ORT", "TRI", "CONT", "RMI");
$FindDoc= array("RMI0000191","ORT0000379","ORT0000391","ORT0000392","ORT0000390","CONT0000274","CONT0000275","RMI0000192","ORT0000391");
$result =[];
foreach($orderDoc as $doc){
$temp = preg_grep("/^". preg_quote($doc) . "/", $FindDoc);
sort($temp);
$result = array_merge($result, $temp);
}
var_dump($result);
$结果:
array(9) {
[0]=>
string(10) "ORT0000379"
[1]=>
string(10) "ORT0000390"
[2]=>
string(10) "ORT0000391"
[3]=>
string(10) "ORT0000391"
[4]=>
string(10) "ORT0000392"
[5]=>
string(11) "CONT0000274"
[6]=>
string(11) "CONT0000275"
[7]=>
string(10) "RMI0000191"
[8]=>
string(10) "RMI0000192"
}
注意:这会保留数组中的重复项。
你不是说应该删除它们,但如果你想删除它们,只需在将 temp 合并到 $result