I need help on how i can specify an array name or key using a variable? I've tried countless times and it never works
Before i get to the code, $games is the array, and i show it at the bottom
here is my code (i will show array example at the bottom)
$increment = 0; //increment i use in a while loop +1 per loop starting at 0
$increment2 = increment - 1; // to get the game number i need to minus 1 from $increment,
//this works since the array starts at 0 and increment will be 1 when the while loop starts.
$gamestotal = $games[totalGames]; //This number is different everytime, it is always
//between 1-1000, it counts the number of a games a person has.
$gamesnpcommid = $games[$increment2][npcommid]; //each game has a unique code, i cURL this
//info from Sony servers and return it in an array (array example below) This is the part
//that is not working, to help understand please check array first, this variable is used
//in the while loop and i need it to start at array 0 and keep returning the unique code
//until the loops stops at the final array number.
//Here is the loop
while($increment<$gamestotal)
if($increment % 2 == 0){
$increment=$increment+1;
echo "<tr>";
echo "<td style='background-color:#e6e8fa'>";
echo $gamesnpcommid;
echo "</td>";
echo "</tr>";
}
else
{$increment=$increment+1;
echo "<tr>";
echo "<td style='background-color:adeaea'>";
echo $gamesnpcommid;
echo "</td>";
echo "</tr>";
}
echo "</table>";
Everything is working completely fine but the array reference is not working ($gamesnpcommid), here is an example of what the array $games can look like:
The array is stored in variable $games,
Array
(
[totalGames] => 110
[0] => Array
(
[npcommid] => NPWR00507_00
[platinum] => 0
[gold] => 1
[silver] => 11
[bronze] => 32
[total] => 44
[lastupdated] => 1329258539
)
[1] => Array
(
[npcommid] => NPWR01140_00
[platinum] => 1
[gold] => 4
[silver] => 8
[bronze] => 29
[total] => 42
[lastupdated] => 1328586022
)
[2] => Array
(
[npcommid] => NPWR01118_00
[platinum] => 0
[gold] => 0
[silver] => 3
[bronze] => 8
[total] => 11
[lastupdated] => 1328067349
)
)
Can someone explain to me why, for example, if $increment2 = 2
and then when i try to reference the array by using $games[$increment2][npcommid] it does not return NPWR01118_00 like in the array?
But when i use $games[2][npcommid] it works?
I've done thorough debugging and i know the array works but i'm stumped at this point.
foreachloop instead to loop over this array?$increment2 = $increment - 1;(missing $) and check the results.