View Full Version : array question

11-21-2007, 09:13 AM
Hi guys,

what's the best way to get a list of results from the DB in to an array? I want to get a default list of items, then perform additional queries on them. eg

[$moduleID][$processID][$metricID] = array("item1"=>$item1, "item2"=>$item2);

is that the right way to do it? or should I use array_push?? I need a hierarchical approach so that it can be used to create a tree view later. an example data set would be

[1][1.01][R11101] ( "item1"=>"test", "item2"=>testy);
[1][0.00][R11100] ( "item1"=>"test", "item2"=>testy);
[1][1.02][R11102] ( "item1"=>"test", "item2"=>testy);
[1][1.03][R11103] ( "item1"=>"test", "item2"=>testy);
[2][2.01][R21101] ( "item1"=>"test", "item2"=>testy);

If I then need to edit a value, how do I refer to it? eg

[$moduleID][$processID][$metricID]["item1"] = "new_test";

11-21-2007, 04:25 PM
I'm assuming the code you posted means you are working with an array with multiple dimensions (arrays of arrays).

Rather than use array index names such as item1, item2, etc, you should probably add yet another level on the array. To use your example:

$arr [1][1.01][R11101][item][1] = "test";
$arr [1][1.01][R11101][item][2] = "testy";
$arr [1][0.00][R11100][item][1] = "test";
$arr [1][0.00][R11100][item][2] = "testy";
$arr [1][1.02][R11102][item][1] = "test";
$arr [1][1.02][R11102][item][2] = "testy";
$arr [1][1.03][R11103][item][1] = "test";
$arr [1][1.03][R11103][item][2] = "testy";
$arr [2][2.01][R21101][item][1] = "test";
$arr [2][2.01][R21101][item][2] = "testy";

And you would refer to it as you suggest, with the added level in there too:

//add a test
$arr [$moduleID][$processID][$metricID]['item'][] = "new_test";
//modify a test
$arr [$moduleID][$processID][$metricID]['item'][$nr] = "new_test";
//delete a test
unset($arr [$moduleID][$processID][$metricID]['item'][$nr]);

11-22-2007, 10:07 AM
cool - thanks for that. I'll give it a go!!