我試圖做一個函數,它能夠通過一個給定的次數旋轉數組,然後返回第一個索引。但我所擁有的真的很慢很笨重。看看:陣列的低效旋轉
<?php
/**
* Get the current userid
* @return integer
*/
public function getCurrentUser(DateTime $startDate, DateInterval $interval, DateTime $endDate, $currentUser, $users, $rotating)
{
if ($rotating == 0)
{
return $currentUser;
}
$usrArray = array();
$dateRange = new DatePeriod($startDate, $interval, $endDate);
// Push userIds to an array
foreach ($users as $user)
{
$usrArray[] = $user->id;
}
// Get the number of iterations from startDate to endDate
$steps = iterator_count($dateRange);
// Find the initial position of the orignal user
$key = array_search($currentUser, $usrArray);
// Set up the array so index 0 == currentUser
$usr = $usrArray;
array_splice($usr, $key);
$slice = array_slice($usrArray, $key);
$startList = array_merge($slice, $usr);
// Start rotating the array
for ($i=0; $i < $steps; $i++)
{
array_push($startList, array_shift($startList));
}
return $startList[0];
}
這是PHP腳本超時之前的Xdebug配置文件。 xdebug profile
有沒有更好的方法來找出誰是索引0後x的旋轉量?
這一翻譯轉了N次,爲什麼不乾脆返回第N指數? –
你能舉個例子嗎?我不太清楚你的意思。 – dadord