Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 6 of 6
  1. #1
    Senior Member jf1288's Avatar
    Join Date
    Sep 2003
    Posts
    215
    Member #
    3300
    I have an array such as this:

    Code:
    Array
    (
        [0] => Array
            (
                [elementName] => opt41
                [id] => 41
                [title] => Bar Option
                [name] => 
                [type] => 1
                [values] => Array
                    (
                        [0] => Bar1
                        [1] => Bar2
                    )
            )
        [1] => Array
            (
                [elementName] => opt40
                [id] => 40
                [title] => FooOption
                [name] => 
                [type] => 1
                [values] => Array
                    (
                        [0] => Foo1
                        [1] => Foo2
                        [2] => Foo3
                    )
            )
        [2] => Array
            (
                [elementName] => opt39
                [id] => 39
                [title] => Option Name
                [name] => 
                [type] => 1
                [values] => Array
                    (
                        [0] => Value1
                        [1] => Value2
                        [2] => Value3
                    )
            )
    )
    And basically, I'm totally stumped on a way of going through each option and returning an array that contains each possible combination of the values for all the options. So in this case I would want to run:

    $combos = build_combos($opts);

    and have $combos be:

    Array(
    [0] => Bar1Foo1Value1,
    [1] => Bar1Foo1Value2,
    [1] => Bar1Foo1Value3,
    [1] => Bar1Foo2Value1,
    [1] => Bar1Foo2Value2,
    [1] => Bar1Foo2Value3,
    [1] => Bar1Foo3Value1,

    and so on.

    can anyone help me make the build_combos function?
    If its not in Family Guy its not worth it.

  2.  

  3. #2
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Not tested:
    PHP Code:
    print_r(build_combos($opts));

    build_combos($opts$combos=null$current=0) {
        
    // Initialize combos for first iteration
        
    if (!is_array($combos)) $combos = array("");

        
    // Get the values
        
    $opt $opts[$current];
        if (
    is_array($opt)) $values $opt['values'];

        
    // Default $values to a single-iteration array
        
    if (!is_array($values) || count($values) == 0$values = array("");

        
    // Initialize new combos (for return)
        
    $newCombos = array();

        
    // Multiplificate combos
        
    foreach ($values as $value) {
            foreach (
    $combos as $combo) {
                
    $newCombos[count($newCombos)] = $combo $value;
            }
        }
        if (
    $currentcount($opts)-1$newCombos build_combos($opts$newCombos$current+1);
        return 
    $newCombos;


  4. #3
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Alternative approach using looping instead of recursion (probably faster):

    PHP Code:
    print_r(build_combos($opts));

    build_combos($opts) {
        
    $combos = array();
        foreach (
    $opts as $opt) {
            
    // Get the values
            
    if (is_array($opt)) $values $opt['values'];
            if (
    is_array($values) && count($values) > 0$combos add_combos($combos$values);
        }
        return 
    $combos;
    }

    add_combos($combos$values) {
        
    // Initialize return value
        
    $outCombos = array();

        
    // Multiplificate combos
        
    foreach ($values as $value) {
            foreach (
    $combos as $combo) {
                
    $outCombos[count($outCombos)] = $combo $value;
            }
        }
        return 
    $outCombos;


  5. #4
    Senior Member jf1288's Avatar
    Join Date
    Sep 2003
    Posts
    215
    Member #
    3300
    this, wow. this is great.

    I used the second option, to avoid recursion, and had to make one change to it, adding


    if(count($combos)==0)
    return $values;


    at the start of add_combos. Without it, it never starts to build the array because it winds up foreach'ing over an empty $combos array.
    If its not in Family Guy its not worth it.

  6. #5
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Quote Originally Posted by jf1288
    this, wow. this is great.
    Happy to help... "will work for :thumbsup:"

  7. #6
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Yes, recursion rocks, but in non-tail-recursively-optimized languages like just about any non-functional language, looping is way cheaper. On a mostly unrelated note, Rubinius, a Ruby runtime, will (or already has, maybe) probably be getting tail recursion optimization. w00t.


Remove Ads

Posting Permissions

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

Search tags for this page

recursion hurts my brain

Click on a term to search for related topics.
All times are GMT -6. The time now is 03:58 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com