Skip to content

Instantly share code, notes, and snippets.

@Gabelbombe
Last active August 29, 2015 14:07
Show Gist options
  • Save Gabelbombe/384a69b44856f50d3c88 to your computer and use it in GitHub Desktop.
Save Gabelbombe/384a69b44856f50d3c88 to your computer and use it in GitHub Desktop.
Build a cartesion array
$attributeValues = [
'color' => ['Red', 'White', 'Blue'],
'fabric' => ['Cloth', 'Silk']
'size' => range(1, 4),
];
/**
* Builds a Cartesian array based of sub array contents
*
* @param $set
* @return array
*/
$build = function ($set) USE (&$build) //pass $build by reference
{
$result = [];
if (! $set) return [ [] ]; //retVal: array of arrays
foreach (array_shift($set) AS $v)
foreach ($build($set) AS $p)
{
array_unshift($p, $v);
$result[] = $p;
}
return $result;
};
print_r($build($attributeValues));
@Gabelbombe
Copy link
Author

outputs

Array
(
    [0] => Array
        (
            [0] => Red
            [1] => Cloth
            [2] => 1
        )

    [1] => Array
        (
            [0] => Red
            [1] => Cloth
            [2] => 2
        )

    [2] => Array
        (
            [0] => Red
            [1] => Cloth
            [2] => 3
        )

    [3] => Array
        (
            [0] => Red
            [1] => Cloth
            [2] => 4
        )

    [4] => Array
        (
            [0] => Red
            [1] => Silk
            [2] => 1
        )

    [5] => Array
        (
            [0] => Red
            [1] => Silk
            [2] => 2
        )

    [6] => Array
        (
            [0] => Red
            [1] => Silk
            [2] => 3
        )

    [7] => Array
        (
            [0] => Red
            [1] => Silk
            [2] => 4
        )

    [8] => Array
        (
            [0] => White
            [1] => Cloth
            [2] => 1
        )

    [9] => Array
        (
            [0] => White
            [1] => Cloth
            [2] => 2
        )

    [10] => Array
        (
            [0] => White
            [1] => Cloth
            [2] => 3
        )

    [11] => Array
        (
            [0] => White
            [1] => Cloth
            [2] => 4
        )

    [12] => Array
        (
            [0] => White
            [1] => Silk
            [2] => 1
        )

    [13] => Array
        (
            [0] => White
            [1] => Silk
            [2] => 2
        )

    [14] => Array
        (
            [0] => White
            [1] => Silk
            [2] => 3
        )

    [15] => Array
        (
            [0] => White
            [1] => Silk
            [2] => 4
        )

    [16] => Array
        (
            [0] => Blue
            [1] => Cloth
            [2] => 1
        )

    [17] => Array
        (
            [0] => Blue
            [1] => Cloth
            [2] => 2
        )

    [18] => Array
        (
            [0] => Blue
            [1] => Cloth
            [2] => 3
        )

    [19] => Array
        (
            [0] => Blue
            [1] => Cloth
            [2] => 4
        )

    [20] => Array
        (
            [0] => Blue
            [1] => Silk
            [2] => 1
        )

    [21] => Array
        (
            [0] => Blue
            [1] => Silk
            [2] => 2
        )

    [22] => Array
        (
            [0] => Blue
            [1] => Silk
            [2] => 3
        )

    [23] => Array
        (
            [0] => Blue
            [1] => Silk
            [2] => 4
        )

)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment