From the course: PHP Tips, Tricks, and Techniques

Unlock this course with a free trial

Join today to access over 22,400 courses taught by industry experts.

Getting all possible permutations of an array

Getting all possible permutations of an array

From the course: PHP Tips, Tricks, and Techniques

Getting all possible permutations of an array

- [David] Hi, I'm David Powers, and welcome to this week's addition of PHP Tips, Trick, and Techniques, designed to help you become a smarter, more productive PHP developer. This week, we'll look at generating all possible permutations of an array. Before looking at the code, let's review how this can be done. Imagine you have an array with three elements, A, B, and C. Focus on the position of the first element, A. Move A between B and C, then move A to the end of the array. That gives us three permutations. Move A back to the start of the array, but reverse the order of B and C. Then, start moving A again, first between C and B, and then to the end of the array. We've now calculated all six possible permutations of the sequence A, B, C. This is a very simple example, but the same principle applies to longer arrays. With the sequence A, B, C, D, A is moved along the array until it reaches the end. Then, the second element, B, is shifted one position, so that it's between C and D. Then…

Contents