I have a table called entries like so
id
total_entries
contest_id
cheater
This function selects the IDs from a list of "entries" and puts them into an array sorted by "total_entries" excluding anyone marked as a "cheater" and also only including entries inside a single "contest_id"
Then it returns a smaller array of 5 ranks.
However, i’m getting out of memory errors when I use this function.
Any thoughts on how to improve it?
public static function five_ranks($id)
{
// $id is the id of the user
$user_search = Entries::findOne($id);
$email = $user_search->email;
$contest_id = $user_search->contest_id;
$find = Entries::find()->where(['contest_id' => $contest_id])->andWhere(['cheater' => 0])->orderBy('total_entries DESC, id DESC')->all();
$arr = array();
foreach($find as $t)
{
$arr[] = $t->id;
}
$total_contestants = count($arr);
//print_r($arr);
$key = array_search($id, $arr); // $key = 2;
//$key = array_search('red', $array);
$five_ranks_array = array();
if (($key) < 5)
{
// he's ranked in the top 6
$five_ranks_array = array_slice($arr, 0, 5);
//return $arr;
return $five_ranks_array;
}
else
{
$x = 1;
while ($x >= 0)
{
$five_ranks_array[] = $arr[$key - $x];
$x--;
}
if ($total_contestants >= 5)
{
$x = 1;
while ($x <= 4)
{
$five_ranks_array[] = $arr[$key + $x];
$x++;
}
}
return $five_ranks_array;
}
//return $five_ranks_array;
}