Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: array question

  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post

    array question

    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

    PHP Code:
    [$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

    PHP Code:
    [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";

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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:

    PHP Code:
    $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:
    PHP Code:
    //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]); 

  • Users who have thanked Fumigator for this post:

    homerUK (11-22-2007)

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    cool - thanks for that. I'll give it a go!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •