Okay, I've been racking my brain trying to figure this one out but the more I try, the more I get stuck.
I need to construct a multi-dimensional array as follows, so for each iteration there are 4 values added to the array. (there is a total of 6 iterations, but here I'm only showing 3):
while($i < 7)
{
if ($i == 1)
{
$arrData[0][1] = "Used";
$arrData[1][1] = "Free";
$arrData[0][2] = $used;
$arrData[1][2] = $free;
}
elseif($i == 2)
{
$arrData[0][3] = "Used";
$arrData[1][3] = "Free";
$arrData[0][4] = $used;
$arrData[1][4] = $free;
}
elseif($i == 3)
{
$arrData[0][5] = "Used";
$arrData[1][5] = "Free";
$arrData[0][6] = $used;
$arrData[1][6] = $free;
}
$i++;
}
This is fairly simple... but it also looks pretty damn ugly.
What I want to do is for a function to handle this (rather than having thirty+ lines of code). So here's my rather rusty logic:
//perform the six iterations
for ($i=1; $i<7; $i++)
{
$free = 'some number';
$used = 'another number';
$array[][] = generateArray($free,$used,$i); //call the function on each iteration
}
function generateArray($free,$used,$i)
{
// My problem is working out how to dynamically generate the correct keys numbers here...
$arrData[0][1] = "Used";
$arrData[1][1] = "Free";
$arrData[0][2] = $used;
$arrData[1][2] = $free;
return $arrData;
}
Can someone please lend me a hand. (the array is used to generate 6 different Pie charts)
Many thanks