I have no reasons for posting this article here. But I’m sure someone can get some insight from this. This is a good example of making use of recursive function calls also known as recursion. It takes the form of a function that calls itself. Recursive functions or recursion are something that needs a little more attention as you have to be sure that the function call finally ends somewhere.
A useful way to think of recursive functions is to imagine them as a process being performed where one of the instructions is to “repeat the process”.
A simple example to show how recursion works would be a function that finds the factorial of an integer.
<?php
function factorial($number) {
if ($number == 0) return 1;
return $number * factorial($number - 1);
}
print factorial(6);
?>
What we need is a function that will accept an integer, and, if that integer is not 0, will call the function again, this time passing in the same number it accepted minus 1, then multiply that result by itself. If it is sounding hard, then you are in for a surprise: the function to calculate factorials is made up of only two lines of code. As you can see, recursive functions make programming certain tasks particularly easy.
Let’s come back to my problem. I have got a number of arrays which has many elements in it. The problem is to find all the possible combination between all the elements of these arrays. You can see below how I solved it in PHP.
I have these arrays. It could be any number of arrays.
$a1 = array(”01″, “02″);
$a2 = array(”white”, “green”);
$a3 = array(”one”, “two”, “three”);
I want this as the output.
01 one green
01 two green
01 three green
01 one white
01 two white
01 three white
02 one green
02 two green
02 three verde
02 one white
02 two white
02 three white
And the solution to this problem in PHP is this.
<?php
$a = array("01", "02");
$b = array("white", "green");
$c = array("one", "two", "three");
$aG = array($a, $b, $c);
$codes = array();
$pos = 0;
generateCodes($aG);
function generateCodes($arr) {
global $codes, $pos;
if(count($arr)) {
for($i=0; $i<count($arr[0]); $i++) {
$tmp = $arr;
$codes[$pos] = $arr[0][$i];
$tarr = array_shift($tmp);
$pos++;
generateCodes($tmp);
}
} else {
echo join(", ", $codes)."<br/>";
}
$pos--;
}
?>
Let me know if you have any questions on this.
ASK a Question! |
|

Hi
Realy u people are doing well.
by using this code i only want to say waaaaaooooooo………
Thanks
Abhi
Thanks for sharing. Here’s the associative version:
header('Content-Type: text/plain');
$a = array("01", "02");
$b = array("white", "green");
$c = array("one", "two", "three");
$aG = array($a, $b, $c);
$aG = array('asdf'=>$a, 'color'=>$b, 'yxcv'=>$c);
$codes = array();
$pos = 0;
generateCodes($aG);
function generateCodes($arr) {
global $codes, $pos;
if (count($arr)) {
for ($i = 0, $c=count($arr[key($arr)]); $i < $c; $i++) {
$tmp = $arr;
$codes[key($arr)] = $arr[key($arr)][$i];
$tarr = array_shift($tmp);
$pos++;
generateCodes($tmp);
}
} else {
print_r($codes);
}
$pos--;
}
Cool Dan!
Thanks,
Anees