Skip to content
Advertisement

PHP – How to get all of possiblities sequence in an array / linked list

Basically I want to create a historical route in my app and I tried SplDoublyLinkedList, but I failed.

Now I decided to just use an array.

Suppose there is an array $array = [0,1,2,3]; . I want to generate all sequences with the following conditions.

JavaScript

Advertisement

Answer

You can reach the desired result by using an outer and an inner loop:

JavaScript

If you intend to generate many sequences and you must consider memory usage, then you can also use a generator:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement