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

Member Avatar for diafol

Sorry, raw arrays do my loaf. The first dimension relates to what? used/free? The second dimension relates to the chart number? So arrData[0][3] means 'Chart 3, Used'?

Why not use the association 'used' and 'free' as keys:

$arrData[$chartno]['used']
$arrData[$chartno]['free']

For me, this seems easier to maintain. However, you seem to be producing 4 values or 2 charts for each 'iteration'. Would this be more in line with what you're trying to achieve?

$arrData[$chartno]['used']
$arrData[$chartno]['free']
$arrData[$chartno]['used_custom']
$arrData[$chartno]['free_custom']

Or are the first two array elements just strings for labels? If so, how about this:

$labels[0] = "Used";
$labels[1] = "Empty";

function generateArray($free, $used){
  return array("used"=>$used,"free"=>$free);
}


$i = 0;
//perhaps $free and $used declared here???
while($i < 6){
//or here???
  $arrData[$i]= generateArray($free, $used);
  $i++;
}

I have to say, I can't really see much of an advantage creating a function over a simple set of conditionals (switch or if/elseif).

Diolch yn fawr ardav!
So, each iteration relates to a disk drive. The charts are used to show the amount of free/used disk space for each of the six drives.
E.g:

if ($i == 1)     // disk drive 1 of 6 
	{
		$arrData[0][1] = "Used";   //This is the label for Gb used
   		$arrData[1][1] = "Free";   //This is the label for Gb remaining
   		$arrData[0][2] = $used;    //This is the no of Gb used
   		$arrData[1][2] = $free;    //No of Gb free
	}

Your idea for using associative labels instead of numbers is good. I'll have a play around when i'm back in the office tomorrow and post my solution. Cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.