php数组排序后保留键

php数组排序后保留键,php,arrays,recursion,key,preserve,Php,Arrays,Recursion,Key,Preserve,我在这里找到了Oezis的这个好的旧算法 它工作得很好,但要使用它,我需要修改它,如果可能的话。 现在,我输入普通数字并接收可能组合的新数组 相反,我需要用数字键输入数字,并用可能的组合保留数字键 提前感谢你的想法 溴 米莱赫 现在: 目标是: // Inputs NEW - with keys to be preserved with the result $n = array('21400'=>11, '10800'=>18, '91380'=>24, '91380'=&

我在这里找到了Oezis的这个好的旧算法

它工作得很好,但要使用它,我需要修改它,如果可能的话。 现在,我输入普通数字并接收可能组合的新数组

相反,我需要用数字键输入数字,并用可能的组合保留数字键

提前感谢你的想法

溴 米莱赫

现在:

目标是:

// Inputs NEW - with keys to be preserved with the result
$n = array('21400'=>11, '10800'=>18, '91380'=>24, '91380'=>24, '21600'=>10, '70600'=>9, '71561'=>2);
// RESULT NEW - new array with old keys (associated with old keys)
$ret = array('21400'=>11, '10800'=>18, '91380'=>24, '70600'=>9);
Oezis提出的算法

函数数组\u和\u部分($n,$t,$all=false){
$count\u n=count($n);//该数组中有多少字段?
$count=pow(2,$count\n);//我们需要做2^个字段的计算来测试所有的可能性
#现在我想看看从1到$count的每个数字,其中数字代表
#数组,并将实际数字所在位置的所有数组元素相加
#有一个1位
#例如:
#$i=1在二进制模式下1=01我将只使用第一个数组元素
对于($i=1;$i1;//位向左移动,以查看下一个循环中的下一位
}
如果($total==$t){
$loesung[$i]=$anzahl;//如果此try的和是我们要查找的和,则将其保存到一个数组中(其中包含用于排序的1位数字)
如果(!$all){
break;//如果我们不是在寻找所有的解决方案,请暂停,因为找到了第一个解决方案
}
}
}
asort($loesung);//根据使用的数字量对所有解决方案进行排序
//格式化获取原始数组键(应该是返回值)的解决方案
foreach($val=>$anzahl){
$bit=strev(decbin($val));
$total=0;
$ret_this=array();

对于($j=0;$j,请看一看。使用arsort()它不适用于此特定的script@oezi,你介意看看这个吗:)
// Inputs NEW - with keys to be preserved with the result
$n = array('21400'=>11, '10800'=>18, '91380'=>24, '91380'=>24, '21600'=>10, '70600'=>9, '71561'=>2);
// RESULT NEW - new array with old keys (associated with old keys)
$ret = array('21400'=>11, '10800'=>18, '91380'=>24, '70600'=>9);
function array_sum_parts($n,$t,$all=false){
    $count_n = count($n); // how much fields are in that array?
    $count = pow(2,$count_n); // we need to do 2^fields calculations to test all possibilities

# now i want to look at every number from 1 to $count, where the number is representing
# the array and add up all array-elements which are at positions where my actual number
# has a 1-bit
# EXAMPLE:
# $i = 1  in binary mode 1 = 01  i'll use ony the first array-element

    for($i=1;$i<=$count;$i++){ // start calculating all possibilities
        $total=0; // sum of this try
        $anzahl=0; // counter for 1-bits in this try
        $k = $i; // store $i to another variable which can be changed during the loop
        for($j=0;$j<$count_n;$j++){ // loop trough array-elemnts
            $total+=($k%2)*$n[$j]; // add up if the corresponding bit of $i is 1
            $anzahl+=($k%2); // add up the number of 1-bits
            $k=$k>>1; //bit-shift to the left for looking at the next bit in the next loop
        }
        if($total==$t){
            $loesung[$i] = $anzahl; // if sum of this try is the sum we are looking for, save this to an array (whith the number of 1-bits for sorting)
            if(!$all){
                break; // if we're not looking for all solutions, make a break because the first one was found
            }
        }
    }

    asort($loesung); // sort all solutions by the amount of numbers used


    // formating the solutions to getting back the original array-keys (which shoud be the return-value)
    foreach($loesung as $val=>$anzahl){
        $bit = strrev(decbin($val));
        $total=0;
        $ret_this = array();
        for($j=0;$j<=strlen($bit);$j++){
            if($bit[$j]=='1'){
                $ret_this[] = $j;
            }   
        }
        $ret[]=$ret_this;
    }

    return $ret;
}

// Target
$t=62;