我试图做一个函数,它能够通过一个给定的次数旋转数组,然后返回第一个索引。但我所拥有的真的很慢很笨重。看看:阵列的低效旋转
<?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