从数组中获取值总计为100的所有键

时间:2016-07-21 09:17:24

标签: php

我有一个这样的数组:

$arr = array(25, 30, 50, 15, 20, 30);

我想要获取值为100的所有键,或者如果不可能总100则意味着它将获取小于100

我需要总共100个

> $array[2]+$array[4]+$array[5]=100

我实际需要的是100种组合。

1 个答案:

答案 0 :(得分:2)

最后我明白了。可能有更好的解决方案,但它有效...

你的问题很复杂。具有6个条目的阵列有720种可能的唯一排列。更多条目意味着更多组合。所以我首先得到了所有独特的permations,然后计算出每个组合的可能性低于或等于100。

$array = array(25, 30, 50, 15, 20, 30);
// 'permutations' function can be found at the bottom of my post
$permutations = permutations(array_keys($array));
$combinations = array();

foreach( $permutations as $keys ) {
    $current = array("sum" => 0, "keys" => array());

    foreach( $keys as $key ) {
        if( $current["sum"] + $array[$key] <= 100 ) {
            $current["sum"] += $array[$key];
            $current["keys"][] = $key;
        }
    }

    // order the keys and create a hash of them
    // to be sure each combination only exists once in the result
    sort($current["keys"]);
    $combinations[md5(join("", $current["keys"]))] = $current;
}

// remove the hashes from array when finished
$combinations = array_values($combinations);

// the final result
print_r($combinations);

对于您的阵列,有11种可能的组合。结果将是:

Array (
    [0] => Array (
        [sum] => 90
        [keys] => Array (
            [0] => 0
            [1] => 1
            [2] => 3
            [3] => 4
        )
    )
    [1] => Array (
        [sum] => 90
        [keys] => Array (
            [0] => 0
            [1] => 2
            [2] => 3
        )
    )
    [2] => Array (
        [sum] => 95
        [keys] => Array (
            [0] => 1
            [1] => 2
            [2] => 3
        )
    )
    [3] => Array (
        [sum] => 95
        [keys] => Array (
            [0] => 0
            [1] => 2
            [2] => 4
        )
    )
    [4] => Array (
        [sum] => 100
        [keys] => Array (
            [0] => 1
            [1] => 2
            [2] => 4
        )
    )
    [5] => Array (
        [sum] => 85
        [keys] => Array (
            [0] => 2
            [1] => 3
            [2] => 4
        )
    )
    [6] => Array (
        [sum] => 100
        [keys] => Array (
            [0] => 0
            [1] => 1
            [2] => 3
            [3] => 5
        )
    )
    [7] => Array (
        [sum] => 95
        [keys] => Array (
            [0] => 2
            [1] => 3
            [2] => 5
        )
    )
    [8] => Array (
        [sum] => 100
        [keys] => Array (
            [0] => 2
            [1] => 4
            [2] => 5
        )
    )
    [9] => Array (
        [sum] => 90
        [keys] => Array (
            [0] => 0
            [1] => 3
            [2] => 4
            [3] => 5
        )
    )
    [10] => Array (
        [sum] => 95
        [keys] => Array (
            [0] => 1
            [1] => 3
            [2] => 4
            [3] => 5
        )
    )
)

使用过的permutations功能基于php cookbook,但改变了您的需求。

function permutations($array, $permutations = array()) {
    if( !empty($array) ) {
        $result = array();

        for( $i = count($array) - 1; $i >= 0; --$i ) {
            $newItems = $array;
            $newPerms = $permutations;
            list($values) = array_splice($newItems, $i, 1);
            array_unshift($newPerms, $values);
            $result = array_merge($result, permutations($newItems, $newPerms));
        }
    }
    else {
        $result = array($permutations);
    }

    return $result;
}