• Home
  • Amoeba
  • ASK
  • Community
  • Source Codes
  • Contact Us
Blue Orange Green Pink Purple

PHP – possible combinations between multiple array elements

Posted in PHP/MySQL, Programming Techniques. on Thursday, January 28th, 2010 by Aneeska
Jan 28

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!

Bookmark and Share

3 Comments

  1. Abhinaya Panigrahi on March 26th, 2010

    Hi

    Realy u people are doing well.
    by using this code i only want to say waaaaaooooooo………

    Thanks
    Abhi

  2. Dan on August 25th, 2010

    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--;
    }

  3. Aneeska on August 25th, 2010

    Cool Dan!

    Thanks,
    Anees



Leave a Reply

CAPTCHA Image CAPTCHA Audio
Refresh Image
  • Search a Topic


  • Got a Question?
      ASK and get a Solution! ASK
  • Categories
    • CakePHP
    • Computers
    • CSS & XHTML
    • Enterprise Web
    • Flash & Action Script
    • Flex
    • Javascript & Libraries
    • Perl Programming
    • PHP/MySQL
    • Programming Techniques
    • Regular Expression
    • Social Media
  • Recent Visitors
  • Recent Articles
    • How to Protect your SWF (AS2 or AS3). Prevent SWF Decompile using SWF Protector
    • PHP – Search in an Array for Values Matching a Pattern – Regex, Wildcard
    • PHP – How to Get Browser Properties and Capabilities – get_browser()
    • CakePHP – Search for records between two dates inclusively
    • Notebook/Laptop hangs frequently/random (on Low Battery when AC power is connected)
    • Swap Values Without Temp or A Third Variable in PHP
    • CakePHP – Auth Login Redirect Problem with Plugins
    • Convert PHP array to Javascript array
    • CakePHP – Accessing a model in AppController or in any Controllers
    • Save Images from Flash – Actionscript 3, Filereference.save, JPGEncoder
  • Archives
    • September 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
  • Tags
      vertically center horizontally align dead center div amoeba ask credit card workbench decryption solutions kiosk questions horizontal center align vertical center align css trick solution css encryption as2 flash regex as3 Regular Expression action script mysql array CakePHP php

  • Donate

      If you find this site helpful, please donate to Amoeba Solution Kiosk. We will be using the money received for conducting technology awareness camps and seminars for school and college students in rural areas of Kerala, India.

  • Links
    • Amoeba Solutions
    • Blog Catalog
  • Home
  • Amoeba
  • ASK
  • Community
  • Source Codes
  • Contact Us

© Copyright Amoeba Solution Kiosk. All rights reserved.
An Amoeba Concept!

Back to Top