L
L
Lorem Ipsum2014-12-20 16:43:10
PHP
Lorem Ipsum, 2014-12-20 16:43:10

Algorithm for enumeration of combinations by blocks?

Good evening.
There is such an array with data (there can be a lot of blocks inside)

[
    [
        {'name': 'id1', 'value': 1991},
        {'name': 'id2', 'value': 1992},
        {'name': 'id3', 'value': 1993},
        {'name': 'id4', 'value': 1994}
    ],
    [
        {'name': 'id5', 'value': 1995},
        {'name': 'id6', 'value': 1996}
    ],
    [
        {'name': 'id7', 'value': 1997},
        {'name': 'id8', 'value': 1998},
        {'name': 'id9', 'value': 1999},
        {'name': 'id10', 'value': 19910},
        {'name': 'id11', 'value': 19911},
        {'name': 'id12', 'value': 19912}
    ]
}

And that's about what I want to do. Those. in fact, take 1 element from each block in a loop and combine them ..
for f in first:
    for s in second:
        for t in third: 
            print f, '.', s, '.', t

Suggest an algorithm for the implementation of this task. Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Don Kaban, 2014-12-20
@donkaban

You, please define the problem. That is, the input data is understandable. What is the output? combinatorial union? All permutations?

M
miki131, 2014-12-20
@miki131

$result = [];
foreach (array_values($array) as $block) {
    $result[] = array_rand($block);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question