I have a PHP game which uses a MySQL table (history) that keeps track of users and how many times they knew the answer like:
username answer
-------- ------
user1 answer1
user2 answer2
user1 answer3
etc. And I want to use this table to print the best players, so I'm trying to implement an array like
public function getBestPlayers($hrows) /*history_rows*/
{
$ray=array();
$hlen = count($hrows);
for($i=0;$i<$hlen;$i++)
{
$curnick = $hrows[$i]['knower'];
$ray[$curnick]; //my attempt to "set" the index
if($ray[$curnick]!=NULL)
{
$ray[$curnick]++;
}
else
{
$ray[$curnick]=1;
}
}//for
$ray = sort($ray);
return $ray;
}//getBestPlayers()
so that in the end I have $ray['user1'] = 2 , $ray['user2'] = 1 etc.
But this gives error "undefined index: user1".
what should I do?
thanks in advance..
isset()function before you even push a new item