1

我有一个看起来像这样的数组

mainArray =>
    [a] =>
        [A]
        [B]
        [C]
    [b] =>
        [D]
        [E]
        [F]
    [c] =>
        [G]
        [H]
        [I]

我想将它们放入这样的数组中:

secondArray =>
    [0] => { [A], [D], [G] }
    [1] => { [A], [E], [G] }
            .
            .
            .
    [n] => { [C], [F], [I] }

我无法弄清楚如何在 $secondArray 中获取正确数量的元素,这些元素以 { [A],[B], .. } 中的某个元素开头。例如,有多少以 [A] 开头。

这就是我认为我必须做的:

secondArray =>
   [0] =>
        [A]

secondArray =>
   [0] =>
        [A]
        [D]

secondArray =>
   [0] =>
        [A]
        [D]
        [G]


secondArray =>
   [0] =>
        [A]
        [D]
        [G]
   [1] =>
        [A]
        [D]
        [H]


secondArray =>
   [0] =>
        [A]
        [D]
        [G]
   [1] =>
        [A]
        [D]
        [H]
   [2] =>
        [A]
        [D]
        [I]

secondArray =>
   [0] =>
        [A]
        [D]
        [G]
   [1] =>
        [A]
        [D]
        [H]
   [2] =>
        [A]
        [D]
        [I]
   [3] =>
        [A]
        [E]
        [G]
   [4] =>
        [A]
        [E]
        [H]
   [5] =>
        [A]
        [E]
        [I]

secondArray =>
   [0] =>
        [A]
        [D]
        [G]
   [1] =>
        [A]
        [D]
        [H]
   [2] =>
        [A]
        [D]
        [I]
   [3] =>
        [A]
        [E]
        [G]
   [4] =>
        [A]
        [E]
        [H]
   [5] =>
        [A]
        [E]
        [I]
   [6] =>
        [A]
        [F]
        [G]
   [7] =>
        [A]
        [F]
        [H]
   [8] =>
        [A]
        [F]
        [I]

等等,但我真的想不出如何实现它......

任何帮助,将不胜感激

4

2 回答 2

1

您可以按顺序生成元素。

要实现迭代器:

-remember the array of input arrays, if there exists a next element.
   and an array of indexes of the same length.
-if any input array is empty, the result set is empty and there is no next (first) element.
-initialise the array of indexes to all zeroes.

To get the next element:
-If you remember there is no next element, fail.
-compute the result:
--start with an empty result
--For each input array and its corresponding index
---Append input[index] to the result
-compute the next set of indexes:
--Iterate the indexes in reverse order. For each index
---Increment the index
---If the index now points past its corresponding input array
----Reset the index to zero
---Else
----Return the result, exiting the function.
-Remember there is no next element
-Return the result (last element).

如果您确实想要一次所有组合,则代码会稍微简化:

-if any input array is empty, the result set is empty.
-initialise the array of indexes to all zeroes.
-compute the result and store in the result set.
-while not done generating results:
--Iterate the indexes in reverse order. For each index
---Increment the index
---If the index now points past its corresponding input array
----Reset the index to zero
---Else
----Compute the result from the current indexes
----Add the result to the result set
----Continue generating the results
--(all indexes were reset to zero) finish generating the results.
-return the result set.
于 2012-09-29T17:56:46.013 回答
1

在红宝石中:

['A', 'B', 'C'].product(['D', 'E', 'F'])
# => [['A', 'D'], ['A', 'E']...

Array#product可以接受多个数组。

于 2013-07-18T23:39:57.487 回答