0

I'm trying to create an array that lists all possible permutations for $invoices with the two other arrays, $services and $formats. I.e. a list of all possible scenarios. The arrays look like this:

$invoices = array(5001, 5002);
$services = array('A', 'B');
$formats = array('L', 'S');

Ultimately I want an array that lists arrays of all possible combinations in this fashion:

Array
(
    [scenario-1] => Array (
        [0] => Array
            (
                [invoice_no] => 5001
                [service] => A
                [format] => L
            )
        [1] => Array
            (
                [invoice_no] => 5002
                [service] => A
                [format] => L
            )
    )
    [scenario-2] => Array (
        [0] => Array
            (
                [invoice_no] => 5001
                [service] => A
                [format] => L
            )
        [1] => Array
            (
                [invoice_no] => 5002
                [service] => A
                [format] => S
            )
    )
    [scenario-3] => Array (
        [0] => Array
            (
                [invoice_no] => 5001
                [service] => A
                [format] => S
            )
        [1] => Array
            (
                [invoice_no] => 5002
                [service] => A
                [format] => S
            )
    )
    [scenario-4] => Array (
        [0] => Array
            (
                [invoice_no] => 5001
                [service] => B
                [format] => L
            )
        [1] => Array
            (
                [invoice_no] => 5002
                [service] => A
                [format] => L
            )
    )
    ...etc...
)

Each scenario needs to be unique and list every invoice_no. The example above should create 16 unique scenarios.

I've tried doing it myself for most of the day but loops like this really aren't my strong point. Any suggestions would be gratefully received! Thank you.

JoshGeake
  • 141
  • 3
  • 13
  • Do you care about the keys? Is there a specific reason you need the structure to be like this? – Dekel May 01 '20 at 14:40
  • @Dekel The `scenario` key isn't important but the inner keys are. It's used for processing later to find the best scenario for the given set of service/format. – JoshGeake May 01 '20 at 14:44
  • If this is the case - the question that marked as dup is good enough for you. I think you can close this one. – Dekel May 01 '20 at 14:45
  • @NicoHaase it's close but it's not quite right because it's creating combinations using the `invoice_no`. In this it doesn't need to do this as such, instead it's using them as the basis for every unique combination of `service` and `format`. – JoshGeake May 01 '20 at 14:48
  • If that does not solve your exact question, you should at least share your attempts – Nico Haase May 01 '20 at 14:52

0 Answers0