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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Processing Arrays

    Hi,

    I need a bit of help processing an array structure.

    PHP Code:
    $array = array(
        
    "ITEM1"    => array(
            
    "SUBITEM1",
            
    "SUBITEM2" => array(
                
    "SUBSUBITEM1",
                
    "SUBSUBITEM2",
            ),
            
    "SUBITEM3",
        ),
        
    "ITEM2"    => array(
            
    "SUBITEM4",
            
    "SUBITEM5",
        ),
    ); 
    I want to process in a loop each item, then subitem and if existing any subsubitem. Thanks for any pointers.

  • #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

  • #3
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry should also say this will be used to build a insert SQL statement. Each item will be assigned to a id, the sub item will know this id and the subsubitem will now its subitem id.

    If that makes senses, SQL it would produce is below.

    Code:
    INSERT INTO `table` (`item_id`, `item`) VALUES ('1', 'ITEM1');
    INSERT INTO `table2` (`sub_item_id`, `item_id`, `sub_item`) VALUES ('1', '1', 'SUBITEM1');
    INSERT INTO `table2` (`sub_item_id`, `item_id`, `sub_item`) VALUES ('2', '1', 'SUBITEM2');
    INSERT INTO `table3` (`subsub_item_id`, `sub_item_id`, `subsub_item`) VALUES ('1', '1', 'SUBSUBITEM1');
    INSERT INTO `table3` (`subsub_item_id`, `sub_item_id`, `subsub_item`) VALUES ('2', '1', 'SUBSUBITEM2');
    INSERT INTO `table2` (`sub_item_id`, `item_id`, `sub_item`) VALUES ('3', '1', 'SUBITEM3');
    INSERT INTO `table` (`item_id`, `item`) VALUES ('2', 'ITEM2');
    INSERT INTO `table2` (`sub_item_id`, `item_id`, `sub_item`) VALUES ('4', '1', 'SUBITEM4');
    INSERT INTO `table2` (`sub_item_id`, `item_id`, `sub_item`) VALUES ('5', '1', 'SUBITEM5');

  • #4
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Found a solution, might be better options, but this works

    PHP Code:
    $i=1;
    $j=1;
    $k=1;
    foreach (
    $array as $key => $value 
    {
        echo 
    "INSERT into table 1 (item_id, item) VALUES ( $i, $key) "."<br>";
        foreach (
    $array[$key] as $key2 => $value2 )
        {
            if (
    is_array($array[$key][$key2]))
            {
                echo 
    "INSERT into table2 (subitem_id, item_id, sub_item) VALUES ( $j, $i, $key2) "."<br>";
                foreach (
    $array[$key][$key2] as $key3 => $value3 )
                {
                    echo 
    "INSERT into table3 (subsubitem_id, sub_item_id, subsub_item) VALUES ( $k, $j, {$array[$key][$key2][$key3]}) "."<br>";
                    
    $k++;
                }
            }
            else
            {
                echo 
    "INSERT into table2 (subitem_id, item_id, sub_item) VALUES ( $j, $i, {$array[$key][$key2]}) "."<br>";
            }
            
    $j++;
        }
        
    $i++;


  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by poyntesm View Post
    Found a solution, might be better options, but this works

    PHP Code:
    $i=1;
    $j=1;
    $k=1;
    foreach (
    $array as $key => $value 
    {
        echo 
    "INSERT into table 1 (item_id, item) VALUES ( $i, $key) "."<br>";
        foreach (
    $array[$key] as $key2 => $value2 )
        {
            if (
    is_array($array[$key][$key2]))
            {
                echo 
    "INSERT into table2 (subitem_id, item_id, sub_item) VALUES ( $j, $i, $key2) "."<br>";
                foreach (
    $array[$key][$key2] as $key3 => $value3 )
                {
                    echo 
    "INSERT into table3 (subsubitem_id, sub_item_id, subsub_item) VALUES ( $k, $j, {$array[$key][$key2][$key3]}) "."<br>";
                    
    $k++;
                }
            }
            else
            {
                echo 
    "INSERT into table2 (subitem_id, item_id, sub_item) VALUES ( $j, $i, {$array[$key][$key2]}) "."<br>";
            }
            
    $j++;
        }
        
    $i++;

    How much depth can you're array have for its dimensions? I'm kinda under the impression that you can go with an infinite depth.
    If so, look into converting this to a recursive function instead, that way you can just call it over and over within the stack and process as you see fit. Logic coverage will be about the same, but you'll need only one loop, a recursion call and a bit of control on it. This one shouldn't be that bad. If you're max is 3 deep like you have in you're example, than you're perfect to stay with what you have.

    I'd actually look at doing some sorting first, and then parsing to the SQL. This will let you execute multiple insertions against a single insert command so that will keep you're requesting down to hopefully speed things up.

    Edit:
    Sorry I should really mention, I'm about to leave work too and don't have my net set up at home yet, so I won't be able to help you writing the function. Only thing I can suggest is that you have two parameters, one for the collection itself (which base case will be a scalar value), and one for its 'parent' (null by default from the looks of it).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #6
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Max is 3 levels deep but i will look to make it recursive for the fun.

    The array will only be processed most likely once as its a initial data load situation. After that all entries will be well controlled via a page and all info known to build SQL.

    But for the initial load I did not want to hardcode values and then spot I missed a entry and have to adjust all linked values.Now I just populate the array and SQL will be handled.

    My PHP is a bit rusty so was interested in some help, seems I should have just let my brain warm up a bit more

    Thanks all.


  •  

    Posting Permissions

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