Click to See Complete Forum and Search --> : put array in a loop


themoon
01-21-2008, 01:53 AM
Hello,
I have the following static code works fine:
$aGraphData = Array

(array('Apples', 25, 'f'),
array('Oranges', 50, 'f'),
array('Limes', 15, 'f'),
array('Grapes', 11, 'f'),
array('Mangos', 32, 'f'),
array('Bannans', 17, 'f'),
array('Star Fruits', 32, 'f'),
array('Pears', 10.5, 'f'),
array('Plums', 10, 'f'),
array('Peaches', 5, 'f'),
);
and i want to make it dynamic, by having the array elements extracted from the database.
So, i made my first try as the following but, it does n't seem true:
$aGraphData = "Array
(";
for ($i=10;$i<=30;$i++) {
$aGraphData.= array('Peaches', $i, 'f').",";
}
$aGraphData.=")";

any idea !
Thanks

NogDog
01-21-2008, 02:01 AM
$aGraphData = array(); // initialize as an empty array
for ($i=10;$i<=30;$i++) {
$aGraphData[] = array('Peaches', $i, 'f'); // use "[]" to add a new element
}

themoon
01-21-2008, 02:15 AM
Thanks NogDog
That made the trick :)