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

How to created mixed arguments function...

This way useful for create mixed arguments function. function foo() { $args = func_get_args(); print_r($args); }   foo(1, 1256, 6, 17); //mixed ...   -------------------Out-...

Change timestamp to custom format...

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

UTF8 string length counter...

The best way to determine the character count of a UTF8 string. function strlen_utf8( $str ) { return mb_strlen( $str, 'UTF-8' ); }   //or... function strlen_utf8( $str ) { $c = 0;...

Detect leap year...

Detect leap year for gregorian and shamsi date. function gLeapYear($year) { if (($year % 4 == 0) and (($year % 100 != 0) or ($year % 400 == 0))) return true; else return ...