Get all subsets of array

If you have a list of items (for example):

$arr = array(
 1 => null,
 2 => 1,
 3 => 2,
 4 => 3,
 5 => 3);

And you need all subsets of item index 1

1 -> 2, 3, 4, 5
2 -> 3, 4, 5
3 -> 4, 5
4 -> 
5 ->

 Our desired function:

function allSubsets($arr) {
    $result = array();
 
    foreach ($arr as $key => $value) {
        if ($value) {
            $result[$value][] = $key;
            $tmpResult = $result;
            foreach ($tmpResult as $index => $list) {
                if ($index >= $key) {
                    break;
                }
 
                foreach ($list as $i => $j) {
                    if ($value == $j) {
                        $result[$index][] = $key;
                    }
                }
            }
        }
    }
 
    return $result;
}

Example: 

$arr = array(
    1 => null,
    2 => 1,
    3 => 2,
    4 => 3,
    5 => 3);
 
print_r(allSubsets($arr));

Result:

Array
(
    [1] => Array
        (
            [0] => 2
            [1] => 3
            [2] => 4
            [3] => 5
        )

    [2] => Array
        (
            [0] => 3
            [1] => 4
            [2] => 5
        )

    [3] => Array
        (
            [0] => 4
            [1] => 5
        )
)

Recommended pages

Delete file function...

If file exists then delete the file. function delFile( $file_name ) { if ( file_exists( $file_name ) ) // Does '$file_name' exist { unlink( $file_name ); return true; } ...

Download a file from web to local...

Use this function to download a file from web to local machine. function WgetFile( $URL, $dir ) { $nomefile = $dir . "/" . basename( $URL ); if ( copy( $URL, $nomefile ) ) { retu...

Number Format...

Format a number with grouped thousands......

Change timestamp to custom format...

Use this function for change timestamp to custom format......