Php 如何获得数组的所有唯一组合,包括空格(项目顺序不相关)
我有这个阵列:Php 如何获得数组的所有唯一组合,包括空格(项目顺序不相关),php,arrays,unique,Php,Arrays,Unique,我有这个阵列: $array = array( 57, 53, 52 ); 我想得到这些数字的所有唯一组合(不相关项目的顺序) 我想要一个大致如下的结果: // 57 // 57, 53 // 57, 53, 52 // 53 // 52 // 52, 57 我正在使用这个函数,但它会返回每个值的组合,因为我不关心它们的顺序,它们都是相同的结果,只是顺序不同: function my_function( $array ) { $combinations = array();
$array = array( 57, 53, 52 );
我想得到这些数字的所有唯一组合(不相关项目的顺序)
我想要一个大致如下的结果:
// 57
// 57, 53
// 57, 53, 52
// 53
// 52
// 52, 57
我正在使用这个函数,但它会返回每个值的组合,因为我不关心它们的顺序,它们都是相同的结果,只是顺序不同:
function my_function( $array ) {
$combinations = array();
$words = sizeof( $array );
$combos = 1;
for( $i = $words; $i > 0; $i-- ) {
$combos *= $i;
}
while( sizeof( $combinations ) < $combos ) {
shuffle($array);
$combo = implode( " ", $array );
if( !in_array( $combo, $combinations ) ) {
$combinations[] = $combo;
}
}
return $combinations;
}
print_r( my_function( $array ) );
函数我的函数($array){
$combines=array();
$words=sizeof($array);
$combos=1;
对于($i=$words;$i>0;$i--){
$combos*=$i;
}
while(sizeof($组合)<$组合){
洗牌($数组);
$combo=内爆(“,$array);
if(!in_数组($combo,$combines)){
$combinations[]=$combo;
}
}
返回$组合;
}
print_r(my_函数($array));
我怎样才能做到这一点呢?
既然顺序无关紧要,我们似乎可以按顺序来完成。从第一个数字开始,找出所有的组合,然后转到第二个数字作为起始数字,依此类推。因为我喜欢递归函数(函数递归是它自己的回报),所以我会这样做:
<?php
function my_function($array){
$combs = [[]]; // adding empty set for better code clarity
sort($array); // sort the array to avoid verbose code to handle duplicate combinations
$set = [];
foreach($array as $index => $element){
$temp = [];
foreach($combs as $curr_comb){
$new_comb = $curr_comb;
$new_comb[] = $element;
$hashed_comb = implode(",",$new_comb);
if(!isset($set[$hashed_comb])){
$temp[] = $new_comb;
$set[$hashed_comb] = true;
}
}
$combs = array_merge($combs,$temp);
}
return array_slice($combs,1); // removing the empty set initially added
}
print_r(my_function([57,53,52]));
print_r(my_function([3,3,3]));
function unique_combinations( $array, $prefix = '' ) {
$combinations = array();
$count = count($array);
// I use a for loop just to count the number of times to run. Since I'm using array_shift, the array will keep getting smaller
for( $i = 0; $i < $count; $i++ ) {
$number = array_shift($array);
// Grab our current result (this number and any prefixes
$this_result = trim( "$prefix $number" );
$combinations[] = $this_result;
// Now, if the array still has numbers in it, run the function on those numbers and combine results
if ( count($array) > 0 ) {
$combinations = array_merge($combinations, unique_combinations( $array, $this_result ) );
}
}
return $combinations;
}
print_r( unique_combinations( [57,58,59] ) );
函数唯一\u组合($array,$prefix=''){
$combines=array();
$count=计数($array);
//我使用for循环只是为了计算运行的次数。因为我使用的是array\u shift,所以数组将不断变小
对于($i=0;$i<$count;$i++){
$number=数组\u移位($array);
//获取当前结果(此数字和任何前缀)
$this_result=trim($prefix$number”);
$combinations[]=$this_结果;
//现在,如果数组中仍然有数字,则对这些数字运行函数并合并结果
如果(计数($array)>0){
$combinations=array_merge($combinations,unique_combinations($array,$this_result));
}
}
返回$组合;
}
印刷品(独特的组合([57,58,59]);
一种类似且非常简短的递归方法,具有一个匿名函数,排序
和一个早期的数组_unique
。为简单起见,值按升序排序:
// all the logic
$getAllCombinations = function($array) {
$result = [];
sort($array);
$getOrderedCombinations = function ($combination_base, &$result) use (&$getOrderedCombinations) {
array_push($result,$combination_base);
if(count($combination_base) > 1) {
foreach($combination_base as $key => $val) {
$newcombo = $combination_base;
unset($newcombo[$key]);
$getOrderedCombinations($newcombo,$result);
}
}
};
$getOrderedCombinations($array,$result);
return array_unique($result,SORT_REGULAR);
};
// execution
$array = array( 57, 53, 52 );
var_dump($getAllCombinations($array));
不相关项目的顺序
=>因此我假设53,57
和57,53
是相同的。是的,对于我的场景,它们是相同的thing@Steveo您可能应该更新您的示例,而不是!2^n个组合相当多…对于递归函数来说,这是一个非常好的示例