我解决了这个问题,请检查:
<?php
//
// $start: starting position to sort in [0, count($xarr) - 1].
//
function sortArrayFromPosition($xarr, $start)
{
//
if($start < 0)
{
return $xarr;
}
elseif($start >= count($xarr))
{
return $xarr;
}
//
// sort the ending elements:
//
$ends = array_slice($xarr, $start, null, true);
asort($ends);
//
// append beginning elements:
//
$i = 0;
foreach($xarr as $key => $value)
{
if($i > $start)
{
$break;
}
$ends[$key] = $value;
$i++;
}
return $ends;
}
//
// main tests:
//
//$pics = array("0"=>"pic-1", "1"=>"pic-2", "2"=>"pic-3", "3"=>"pic-4", "4"=>"pic-5");
$pics0 = array("0"=>"pic-1", "1"=>"pic-5", "2"=>"pic-3", "3"=>"pic-2", "4"=>"pic-4");
//
$start = 2;
$pics = sortArrayFromPosition($pics0, $start);
//
echo "<pre>";
print_r($pics);
echo "</pre>";
//
$start = 4;
$pics = sortArrayFromPosition($pics0, $start);
//
echo "<pre>";
print_r($pics);
echo "</pre>";
?>