I’ve been banging my head against this problem for a while. I feel like it should be simple, but I’m having a hard time coming up with a solution.
I’m looking to pre-populate a database, and I need to create SQL statements with some foreign key values. It would be tedious to hand-code them, so naturally I decided to do it in code.
What I want are series of arrays that have values as such:
[1]
[2]
[3]
[1,1]
[1,2]
[1,3]
[2,1]
[2,2]
...
[1,1,1]
[1,1,2]
[1,1,3]
...
[3,1,1]
...
[3,3,3]
I want to specify the number of values in the array, and the numerical value at which it causes the preceeding value to roll over.
In the example I gave above, it would be like generate(3,3)
, since the maximum number of elements is 3, and the highest value is 3.
How could I write some code that would give me this series of arrays?
2
Answers
This is a recursive function that will generate each of the combinations of the ranges up to the maximum value, with elements in each array from 1 to the number specified:
Output is too long to show here but can be seen in this demo on 3v4l.org
Note for PHP < 7.4, replace
with
Here’s a version using generators (which may be slightly easier on memory than pure arrays):
Exemple usage + debug/print:
Demo