1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Recursive function help

Discussion in 'PHP' started by stuart, Jun 17, 2009.

  1. #1
    Hi

    I need some help on how to make the following recursive.

    <pre>
    <?php
    $array[1] = array("1a", "1b", "1c");
    $array[2] = array("2a", "2b", "2c");
    $array[3] = array("3a", "3b", "3c");
    
    foreach ($array[1] as $value1 ){
    	foreach ($array[2] as $value2 ){
    		foreach ($array[3] as $value3 ){
    			$result[] = $value1." ".$value2." ".$value3;
    		}
    	}
    }
    print_r($result);
    ?>
    </pre>
    PHP:
    OK so that returns the 27 combinations but I need to occasionally input..
    $array[1] = array("1a", "1b", "1c");
    $array[2] = array("2a", "2b", "2c");
    $array[3] = array("3a", "3b", "3c");
    $array[4] = array("4a", "4b", "4c", "4d");
    PHP:
    or say just..

    $array[1] = array("1a", "1b", "1c");
    $array[2] = array("2a", "2b");
    
    PHP:
    Thanks in advance.
    Stuart
     
    stuart, Jun 17, 2009 IP
  2. stuart

    stuart Guest

    Messages:
    184
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Found the answer here...

    tomscottmalden.wordpress.com/2009/04/20/php-recursive-iterations-in-arrays/

    and here..

    ask.metafilter.com/64554/Finding-combinations-in-PHP

    Thanks
     
    stuart, Jun 18, 2009 IP