how to generate all sub-array of an array

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 12
Joined: Mon Nov 28, 2011 3:53 am

Wed Oct 17, 2012 6:14 pm

Hi everyone,

I need to generate all sub-array from an array.

For example:
source_array = array("a", "b", "c", "d");

so the result is:

array("a"), array("b"), array("c"), array("d"),
array("a", "b"), array("a", "c"), array("a", "d"),
array("b", "c"), array("b", "d"),
array("c", "d"),

array("a", "b", "c"), array("a", "b", "d"),
array("a", "c", "d"),
array("b", "c", "d"),

array("a", "b", "c", "d")

Please suggest me a good way to do that! Thanks all guys!

php-forum Fan User
php-forum Fan User
Posts: 974
Joined: Mon Oct 01, 2012 12:32 pm

Thu Oct 18, 2012 7:17 am

What you're trying to generate, it sounds like, are all permutations of an array. Search for that phrase on the forum and I think you'll find what you're looking for

Post Reply