0

I have a form that contains three sets of checkboxes: Toppings, Sizes, Modifiers. When submitted, each returns an array of values. What I eventually need to do is create a separate record in a database for each possible combo of the three.

Example:

[15-Jul-2016 11:47:29] topps
[15-Jul-2016 11:47:29] Array
(
    [0] => B
    [1] => K
    [2] => KT
    [3] => ME
    [4] => PK
)

[15-Jul-2016 11:47:29] mods
[15-Jul-2016 11:47:29] Array
(
    [0] => R
)

[15-Jul-2016 11:47:29] sizes
[15-Jul-2016 11:47:29] Array
(
    [0] => 7I
    [1] => FM
    [2] => L
    [3] => LP
    [4] => NL
)

I'll need to create a record for B R 7I, B R FM, B R L, etc. K R 7I, K R FM, and so on. What I'd like to have to begin with is an array that looks like this:

$records = array(
    array('B', 'R', '7I'),
    array('B', 'R', 'FM'),
   // and so on
);

I'm not super-familiar with the array functions in PHP (array_walk, array_map, etc.)

Is there any way to generate my desired array without needing to do nested loops?

EmmyS
  • 11,892
  • 48
  • 101
  • 156
  • duplicate: http://stackoverflow.com/questions/8567082/how-to-generate-in-php-all-combinations-of-items-in-multiple-arrays edit: sorry, no answers in this question without looping – Jacob Mulquin Jul 15 '16 at 17:01
  • 2
    why no loops? either you use loops, or you have to write out every possible combination in advance yourself. And internally, array_walk/array_map are using loops ANYWAYS. – Marc B Jul 15 '16 at 17:07
  • Create 3 temporary tables and make outer join of them :) – splash58 Jul 15 '16 at 17:13
  • 1
    just curious, What is your objection to using loops? You are not the first person to state this as a requirement. Where are you getting the advice from to not use loops? Is it all loops or certain types of loops? – Ryan Vincent Jul 15 '16 at 18:00
  • I don't really have a specific objection to loops; I was just wondering if there was another way to do it, perhaps in fewer lines. – EmmyS Jul 15 '16 at 20:28

1 Answers1

0

You could use directly array_walk

$topps = ['B', 'K', 'KT', 'ME', 'PK'];
$mods = ['R'];
$sizes = ['7I', 'FM', 'L', 'LP', 'NL'];

$result = [];

// use maddness
array_walk($topps, function($topp)use($mods, $sizes, &$result) {
    array_walk($mods, function($mod)use($topp, $sizes, &$result) {
        array_walk($sizes, function($size)use($topp, $mod, &$result) {
            $result[] = [$topp, $mod, $size];
        });
    });
});
var_dump($result);

Because of closure system in php you see use madness. You could mitigate by using class, but having class overhead.

// class
class arrayGenerator {

    private $topps, $mods, $sizes;
    private $result;

    function __construct($topps, $mods, $sizes) {
        $this->topps = $topps;
        $this->mods = $mods;
        $this->sizes = $sizes;
    }

    function generate($topps, $mods, $sizes) {
        $this->result = [];
        array_walk($this->topps, function($topp) {
            array_walk($this->mods, function($mod)use($topp) {
                array_walk($this->sizes, function($size)use($topp, $mod) {
                    $this->result[] = [$topp, $mod, $size];
                });
            });
        });
        return $this->result;
    }

}

$generator = new arrayGenerator($topps, $mods, $sizes);
$result = $generator->generate();
var_dump($result);
sectus
  • 15,605
  • 5
  • 55
  • 97