我正在编写一个模块,用其他文本替换部分文本。这是Web应用程序的“重命名”功能。因为我可能将“Actions”重命名为“abcii”,将“Action”重命名为“abci”,我需要对其进行排序,以便包含其他替换短语的短语在它们包含的短语之前位于数组中。
带示例的代码
private function sortStringContainment($a, $b)
{
if ($a == $b){
return 0;
}
return ( stripos( $a, $b ) !== false ) ? -1 : 1;
}
$data = "a:14:{s:7:"Purpose";a:2:{s:11:"defaultName";s:7:"Purpose";s:11:"customLabel";s:9:"Purpose23";}s:6:"Vision";a:2:{s:11:"defaultName";s:6:"Vision";s:11:"customLabel";s:7:"Vision2";}s:18:"Vision Goal (bhag)";a:2:{s:11:"defaultName";s:18:"Vision Goal (bhag)";s:11:"customLabel";s:3:"VG2";}s:17:"Vision Milestones";a:2:{s:11:"defaultName";s:17:"Vision Milestones";s:11:"customLabel";s:3:"VM2";}s:13:"Core Strategy";a:2:{s:11:"defaultName";s:13:"Core Strategy";s:11:"customLabel";s:3:"CS2";}s:4:"Goal";a:2:{s:11:"defaultName";s:4:"Goal";s:11:"customLabel";s:5:"Goal2";}s:5:"Goals";a:2:{s:11:"defaultName";s:5:"Goals";s:11:"customLabel";s:6:"Goals2";}s:9:"Milestone";a:2:{s:11:"defaultName";s:9:"Milestone";s:11:"customLabel";s:10:"Milestone2";}s:10:"Milestones";a:2:{s:11:"defaultName";s:10:"Milestones";s:11:"customLabel";s:11:"Milestones2";}s:15:"Annual Strategy";a:2:{s:11:"defaultName";s:15:"Annual Strategy";s:11:"customLabel";s:3:"AS2";}s:6:"Action";a:2:{s:11:"defaultName";s:6:"Action";s:11:"customLabel";s:7:"Action2";}s:7:"Actions";a:2:{s:11:"defaultName";s:7:"Actions";s:11:"customLabel";s:8:"Actions2";}s:4:"Task";a:2:{s:11:"defaultName";s:4:"Task";s:11:"customLabel";s:5:"Task2";}s:5:"Tasks";a:2:{s:11:"defaultName";s:5:"Tasks";s:11:"customLabel";s:6:"Tasks2";}}";
ksort( $data );
array_reverse( $data );
uksort ( $data, 'sortStringContainment' );
print_r( $data );
我无法弄清楚为什么“任务”总是在“任务”之后结束,我之前需要它。
答案 0 :(得分:-1)
根据@zerkms建议我调整了http://www.codecodex.com/wiki/Merge_sort#PHP
的合并排序导致此代码
function merge_sort(&$arrayToSort)
{
if (sizeof($arrayToSort) <= 1)
return $arrayToSort;
// split our input array into two halves
// left...
$leftFrag = array_slice($arrayToSort, 0, (int)(count($arrayToSort)/2));
// right...
$rightFrag = array_slice($arrayToSort, (int)(count($arrayToSort)/2));
// RECURSION
// split the two halves into their respective halves...
$leftFrag = merge_sort($leftFrag);
$rightFrag = merge_sort($rightFrag);
$returnArray = merge($leftFrag, $rightFrag);
return $returnArray;
}
function merge(&$lF, &$rF)
{
$result = array();
// while both arrays have something in them
while (count($lF)>0 && count($rF)>0) {
if (stripos( $lF[0], $rF[0] ) !== false) {
array_push($result, array_shift($lF));
}
else {
array_push($result, array_shift($rF));
}
}
// did not see this in the pseudo code,
// but it became necessary as one of the arrays
// can become empty before the other
array_splice($result, count($result), 0, $lF);
array_splice($result, count($result), 0, $rF);
return $result;
}
$data = "a:14:{s:7:"Purpose";a:2:{s:11:"defaultName";s:7:"Purpose";s:11:"customLabel";s:9:"Purpose23";}s:6:"Vision";a:2:{s:11:"defaultName";s:6:"Vision";s:11:"customLabel";s:7:"Vision2";}s:18:"Vision Goal (bhag)";a:2:{s:11:"defaultName";s:18:"Vision Goal (bhag)";s:11:"customLabel";s:3:"VG2";}s:17:"Vision Milestones";a:2:{s:11:"defaultName";s:17:"Vision Milestones";s:11:"customLabel";s:3:"VM2";}s:13:"Core Strategy";a:2:{s:11:"defaultName";s:13:"Core Strategy";s:11:"customLabel";s:3:"CS2";}s:4:"Goal";a:2:{s:11:"defaultName";s:4:"Goal";s:11:"customLabel";s:5:"Goal2";}s:5:"Goals";a:2:{s:11:"defaultName";s:5:"Goals";s:11:"customLabel";s:6:"Goals2";}s:9:"Milestone";a:2:{s:11:"defaultName";s:9:"Milestone";s:11:"customLabel";s:10:"Milestone2";}s:10:"Milestones";a:2:{s:11:"defaultName";s:10:"Milestones";s:11:"customLabel";s:11:"Milestones2";}s:15:"Annual Strategy";a:2:{s:11:"defaultName";s:15:"Annual Strategy";s:11:"customLabel";s:3:"AS2";}s:6:"Action";a:2:{s:11:"defaultName";s:6:"Action";s:11:"customLabel";s:7:"Action2";}s:7:"Actions";a:2:{s:11:"defaultName";s:7:"Actions";s:11:"customLabel";s:8:"Actions2";}s:4:"Task";a:2:{s:11:"defaultName";s:4:"Task";s:11:"customLabel";s:5:"Task2";}s:5:"Tasks";a:2:{s:11:"defaultName";s:5:"Tasks";s:11:"customLabel";s:6:"Tasks2";}}";
$sortValues = array_keys( $data );
$this->merge_sort( $sortValues );
$sortValues = array_reverse( $sortValues );
$returnData = array();
foreach ($sortValues as $value){
$returnData[$value] = $data[$value];
}
$data = $returnData;
print_r( $data );