我有一个php脚本,可以对2个多维数组进行排序。现在,如果我使用普通array_multi-sort,它会将某些行的2个数组混淆,因为这两个数组都有一些棘手的元素
例如:a[2][2]='35'b[2][2]='035'
此外,a[3][4]='+0.03'b[3][4]='+0.03'
a[4][6]='31-12-2012'b[4][6]='12/31/2012'
由于上述棘手的元素差异,两个数组的排序结果不同。我需要它们进行适当的排序,因为为了进行分析,我的req要在每行中进行一次匹配。
请建议我如何对这些数组进行"智能"排序,以获得相同的结果
我在的线路上试过一些东西
function sort2dArray($data) {
$tmp = array();
$lineCount = count($data);
foreach ($data as $lineNum => $lineData) {
foreach ($lineData as $column => $value) {
$tmp[$column][$lineNum] = standardize($value);
}
}
$multiSortArgs = array();
foreach ($tmp as $column => $columnData) {
array_push($multiSortArgs, $columnData, SORT_ASC);
}
$multiSortArgs[] = &$data;
call_user_func_array('array_multisort', $multiSortArgs);
return $data;
}
function standardize($value)
{
if(is_numeric($value))
$pval= $value + 0;
else
$pval= $value;
return $pval;
}
更新:以","作为分隔符的两个数组中的o/p。。。仅考虑0035和35情况
BA2063,,,110Y,EFW
BA2063,,,110Y,EFW
BA2063,,,110Y,EFW
BA2063,,,121H,ECD
BA2063,,,121H,ECD
BA2063,,,121H,ECD
BA2063,,,0035,ESW
BA2063,,,0035,EFW
BA2063,,,0035,EFW
BA2063,,,0035,EFW
BA2063,,,1L9M,EFW
BA2063,,,1L9M,EFW
BA2063,,,35,ESW
BA2063,,,35,EFW
BA2063,,,35,EFW
BA2063,,,35,EFW
BA2063,,,110Y,EFW
BA2063,,,110Y,EFW
BA2063,,,110Y,EFW
BA2063,,,121H,ECD
BA2063,,,121H,ECD
BA2063,,,121H,ECD
您的问题很难理解,因为您有多类型的数据和多格式的日期。如果你有混合日期格式,我建议你用这种函数过滤你的数组:
$authorizedFormats= ['Y-m-d','m/d/Y'];//give all your format, be careful inside the order to avoid abiguity mess
$filterVarOptions =['options'=>function($var)use($authorizedFormats){
foreach($authorizedFormats as $value){
$date = DateTime::createFromFormat($value,$var);
if($date !==false){
return $date;
}
}
return new DateTime('01/01/70');
}];
array_walk($yourMultiDimArray,function($var)use($filterVarOptions){
foreach($var as &$value)
$value = filter_var($value,FILTER_CALLBACK,$filterVarOptions);
});