给出一个字符串,如“BARNYARD,FARMER,FOX,HEN,TRACTOR”(注意字符串按字母顺序排列)
如何循环遍历该字符串并在下面创建一个数组str []?请注意,数组键也按字母顺序排列,这有助于消除我认为的重复的两个或X-num字连接?
str[BARNYARD] = BARNYARD
str[FARMER] = FARMER
str[FOX] = FOX
str[HEN] = HEN
str[TRACTOR] = TRACTOR
str[BARNYARDFARMER] = BARNYARD FARMER
str[BARNYARDFOX] = BARNYARD FOX
str[BARNYARDHEN] = BARNYARD HEN
str[BARNYARDTRACTOR] = BARNYARD TRACTOR
str[FARMERFOX] = FARMER FOX
str[FARMERHEN] = FARMER HEN
str[FARMERTRACTOR] = FARMER TRACTOR
str[FOXHEN] = FOX HEN
str[FOXTRACTOR] = FOX TRACTOR
str[HENTRACTOR] = HEN TRACTOR
str[BARNYARDFARMERFOX] = BARNYARD FARMER FOX
str[BARNYARDFOXHEN] = BARNYARD FOX HEN
str[BARNYARDHENTRACTOR] = BARNYARD HEN TRACTOR
str[FARMERFOXHEN] = FARMER FOX HEN
str[FARMERHENTRACTOR] = FARMER HEN TRACTOR
str[FOXHENTRACTOR] = FOX HEN TRACTOR
str[BARNYARDFARMERFOXHEN] = BARNYARD FARMER FOX HEN
str[BARNYARDFOXHENTRACTOR] = BARNYARD FOX HEN TRACTOR
str[FARMERFOXHENTRACTOR] = FARMER FOX HEN TRACTOR
str[BARNYARDFARMERFOXHENTRACTOR] = BARNYARD FARMER FOX HEN TRACTOR
答案 0 :(得分:1)
所以你想要字符串中单词的所有排列吗?
刚刚对PHP网站上的这段代码做了一个小的后期处理:http://www.php.net/manual/en/function.shuffle.php#90615
$in = explode( ',', str_replace( ' ', '', 'BARNYARD, FARMER, FOX, HEN, TRACTOR' ) );
$power_perms = power_perms( $in );
$temp = array();
foreach( $power_perms as $sets )
{
$temp[ implode( '', $sets) ] = implode( ' ', $sets );
}
asort($temp);
print '<pre>'. print_r($temp, true) .'</pre>';
function power_perms( $arr )
{
$power_set = power_set( $arr );
$result = array();
foreach( $power_set as $set ) {
$perms = perms( $set );
$result = array_merge( $result, $perms );
}
return $result;
}
function power_set( $in, $minLength = 1 )
{
$count = count( $in );
$members = pow( 2, $count );
$return = array();
for( $i = 0; $i < $members; $i++ ) {
$b = sprintf( "%0" . $count . "b", $i );
$out = array();
for( $j = 0; $j < $count; $j++ ) {
if( $b{$j} == '1' )
$out[] = $in[$j];
}
if( count( $out ) >= $minLength ) {
$return[] = $out;
}
}
//usort($return,"cmp"); //can sort here by length
return $return;
}
function factorial( $int )
{
if( $int < 2 ) {
return 1;
}
for( $f = 2; $int - 1 > 1; $f *= $int-- )
;
return $f;
}
function perm( $arr, $nth = null )
{
if( $nth === null ) {
return perms( $arr );
}
$result = array();
$length = count( $arr );
while( $length-- ) {
$f = factorial( $length );
$p = floor( $nth / $f );
$result[] = $arr[$p];
array_delete_by_key( $arr, $p );
$nth -= $p * $f;
}
$result = array_merge( $result, $arr );
return $result;
}
function perms( $arr )
{
$p = array();
for( $i = 0; $i < factorial( count( $arr ) ); $i++ ) {
$p[] = perm( $arr, $i );
}
return $p;
}
function array_delete_by_key( &$array, $delete_key, $use_old_keys = FALSE )
{
unset($array[$delete_key]);
if( !$use_old_keys ) {
$array = array_values( $array );
}
return TRUE;
}
示例输出:
Array
(
[BARNYARD] => BARNYARD
[BARNYARDFARMER] => BARNYARD FARMER
[BARNYARDFARMERFOX] => BARNYARD FARMER FOX
[BARNYARDFARMERFOXHEN] => BARNYARD FARMER FOX HEN
[BARNYARDFARMERFOXHENTRACTOR] => BARNYARD FARMER FOX HEN TRACTOR
[BARNYARDFARMERFOXTRACTOR] => BARNYARD FARMER FOX TRACTOR
[BARNYARDFARMERFOXTRACTORHEN] => BARNYARD FARMER FOX TRACTOR HEN
[BARNYARDFARMERHEN] => BARNYARD FARMER HEN
[BARNYARDFARMERHENFOX] => BARNYARD FARMER HEN FOX
[BARNYARDFARMERHENFOXTRACTOR] => BARNYARD FARMER HEN FOX TRACTOR
[BARNYARDFARMERHENTRACTOR] => BARNYARD FARMER HEN TRACTOR
[BARNYARDFARMERHENTRACTORFOX] => BARNYARD FARMER HEN TRACTOR FOX
[BARNYARDFARMERTRACTOR] => BARNYARD FARMER TRACTOR
[BARNYARDFARMERTRACTORFOX] => BARNYARD FARMER TRACTOR FOX
[BARNYARDFARMERTRACTORFOXHEN] => BARNYARD FARMER TRACTOR FOX HEN
[BARNYARDFARMERTRACTORHEN] => BARNYARD FARMER TRACTOR HEN
[BARNYARDFARMERTRACTORHENFOX] => BARNYARD FARMER TRACTOR HEN FOX
[BARNYARDFOX] => BARNYARD FOX
[BARNYARDFOXFARMER] => BARNYARD FOX FARMER
[BARNYARDFOXFARMERHEN] => BARNYARD FOX FARMER HEN
[BARNYARDFOXFARMERHENTRACTOR] => BARNYARD FOX FARMER HEN TRACTOR
[BARNYARDFOXFARMERTRACTOR] => BARNYARD FOX FARMER TRACTOR
[BARNYARDFOXFARMERTRACTORHEN] => BARNYARD FOX FARMER TRACTOR HEN
[BARNYARDFOXHEN] => BARNYARD FOX HEN
[BARNYARDFOXHENFARMER] => BARNYARD FOX HEN FARMER
[BARNYARDFOXHENFARMERTRACTOR] => BARNYARD FOX HEN FARMER TRACTOR
[BARNYARDFOXHENTRACTOR] => BARNYARD FOX HEN TRACTOR
[BARNYARDFOXHENTRACTORFARMER] => BARNYARD FOX HEN TRACTOR FARMER
[BARNYARDFOXTRACTOR] => BARNYARD FOX TRACTOR
[BARNYARDFOXTRACTORFARMER] => BARNYARD FOX TRACTOR FARMER
[BARNYARDFOXTRACTORFARMERHEN] => BARNYARD FOX TRACTOR FARMER HEN
[BARNYARDFOXTRACTORHEN] => BARNYARD FOX TRACTOR HEN
[BARNYARDFOXTRACTORHENFARMER] => BARNYARD FOX TRACTOR HEN FARMER
[BARNYARDHEN] => BARNYARD HEN
[BARNYARDHENFARMER] => BARNYARD HEN FARMER
[BARNYARDHENFARMERFOX] => BARNYARD HEN FARMER FOX
[BARNYARDHENFARMERFOXTRACTOR] => BARNYARD HEN FARMER FOX TRACTOR
[BARNYARDHENFARMERTRACTOR] => BARNYARD HEN FARMER TRACTOR
[BARNYARDHENFARMERTRACTORFOX] => BARNYARD HEN FARMER TRACTOR FOX
[BARNYARDHENFOX] => BARNYARD HEN FOX
[BARNYARDHENFOXFARMER] => BARNYARD HEN FOX FARMER
[BARNYARDHENFOXFARMERTRACTOR] => BARNYARD HEN FOX FARMER TRACTOR
[BARNYARDHENFOXTRACTOR] => BARNYARD HEN FOX TRACTOR
[BARNYARDHENFOXTRACTORFARMER] => BARNYARD HEN FOX TRACTOR FARMER
[BARNYARDHENTRACTOR] => BARNYARD HEN TRACTOR
[BARNYARDHENTRACTORFARMER] => BARNYARD HEN TRACTOR FARMER
[BARNYARDHENTRACTORFARMERFOX] => BARNYARD HEN TRACTOR FARMER FOX
[BARNYARDHENTRACTORFOX] => BARNYARD HEN TRACTOR FOX
[BARNYARDHENTRACTORFOXFARMER] => BARNYARD HEN TRACTOR FOX FARMER
[BARNYARDTRACTOR] => BARNYARD TRACTOR
[BARNYARDTRACTORFARMER] => BARNYARD TRACTOR FARMER
[BARNYARDTRACTORFARMERFOX] => BARNYARD TRACTOR FARMER FOX
[BARNYARDTRACTORFARMERFOXHEN] => BARNYARD TRACTOR FARMER FOX HEN
[BARNYARDTRACTORFARMERHEN] => BARNYARD TRACTOR FARMER HEN
[BARNYARDTRACTORFARMERHENFOX] => BARNYARD TRACTOR FARMER HEN FOX
[BARNYARDTRACTORFOX] => BARNYARD TRACTOR FOX
[BARNYARDTRACTORFOXFARMER] => BARNYARD TRACTOR FOX FARMER
[BARNYARDTRACTORFOXFARMERHEN] => BARNYARD TRACTOR FOX FARMER HEN
[BARNYARDTRACTORFOXHEN] => BARNYARD TRACTOR FOX HEN
[BARNYARDTRACTORFOXHENFARMER] => BARNYARD TRACTOR FOX HEN FARMER
[BARNYARDTRACTORHEN] => BARNYARD TRACTOR HEN
[BARNYARDTRACTORHENFARMER] => BARNYARD TRACTOR HEN FARMER
[BARNYARDTRACTORHENFARMERFOX] => BARNYARD TRACTOR HEN FARMER FOX
[BARNYARDTRACTORHENFOX] => BARNYARD TRACTOR HEN FOX
[BARNYARDTRACTORHENFOXFARMER] => BARNYARD TRACTOR HEN FOX FARMER
[FARMER] => FARMER
[FARMERBARNYARD] => FARMER BARNYARD
[FARMERBARNYARDFOX] => FARMER BARNYARD FOX
[FARMERBARNYARDFOXHEN] => FARMER BARNYARD FOX HEN
[FARMERBARNYARDFOXHENTRACTOR] => FARMER BARNYARD FOX HEN TRACTOR
[FARMERBARNYARDFOXTRACTOR] => FARMER BARNYARD FOX TRACTOR
[FARMERBARNYARDFOXTRACTORHEN] => FARMER BARNYARD FOX TRACTOR HEN
[FARMERBARNYARDHEN] => FARMER BARNYARD HEN
[FARMERBARNYARDHENFOX] => FARMER BARNYARD HEN FOX
[FARMERBARNYARDHENFOXTRACTOR] => FARMER BARNYARD HEN FOX TRACTOR
[FARMERBARNYARDHENTRACTOR] => FARMER BARNYARD HEN TRACTOR
[FARMERBARNYARDHENTRACTORFOX] => FARMER BARNYARD HEN TRACTOR FOX
[FARMERBARNYARDTRACTOR] => FARMER BARNYARD TRACTOR
[FARMERBARNYARDTRACTORFOX] => FARMER BARNYARD TRACTOR FOX
[FARMERBARNYARDTRACTORFOXHEN] => FARMER BARNYARD TRACTOR FOX HEN
[FARMERBARNYARDTRACTORHEN] => FARMER BARNYARD TRACTOR HEN
[FARMERBARNYARDTRACTORHENFOX] => FARMER BARNYARD TRACTOR HEN FOX
[FARMERFOX] => FARMER FOX
[FARMERFOXBARNYARD] => FARMER FOX BARNYARD
[FARMERFOXBARNYARDHEN] => FARMER FOX BARNYARD HEN
[FARMERFOXBARNYARDHENTRACTOR] => FARMER FOX BARNYARD HEN TRACTOR
[FARMERFOXBARNYARDTRACTOR] => FARMER FOX BARNYARD TRACTOR
[FARMERFOXBARNYARDTRACTORHEN] => FARMER FOX BARNYARD TRACTOR HEN
[FARMERFOXHEN] => FARMER FOX HEN
[FARMERFOXHENBARNYARD] => FARMER FOX HEN BARNYARD
[FARMERFOXHENBARNYARDTRACTOR] => FARMER FOX HEN BARNYARD TRACTOR
[FARMERFOXHENTRACTOR] => FARMER FOX HEN TRACTOR
[FARMERFOXHENTRACTORBARNYARD] => FARMER FOX HEN TRACTOR BARNYARD
[FARMERFOXTRACTOR] => FARMER FOX TRACTOR
[FARMERFOXTRACTORBARNYARD] => FARMER FOX TRACTOR BARNYARD
[FARMERFOXTRACTORBARNYARDHEN] => FARMER FOX TRACTOR BARNYARD HEN
[FARMERFOXTRACTORHEN] => FARMER FOX TRACTOR HEN
[FARMERFOXTRACTORHENBARNYARD] => FARMER FOX TRACTOR HEN BARNYARD
[FARMERHEN] => FARMER HEN
[FARMERHENBARNYARD] => FARMER HEN BARNYARD
[FARMERHENBARNYARDFOX] => FARMER HEN BARNYARD FOX
[FARMERHENBARNYARDFOXTRACTOR] => FARMER HEN BARNYARD FOX TRACTOR
[FARMERHENBARNYARDTRACTOR] => FARMER HEN BARNYARD TRACTOR
[FARMERHENBARNYARDTRACTORFOX] => FARMER HEN BARNYARD TRACTOR FOX
[FARMERHENFOX] => FARMER HEN FOX
[FARMERHENFOXBARNYARD] => FARMER HEN FOX BARNYARD
[FARMERHENFOXBARNYARDTRACTOR] => FARMER HEN FOX BARNYARD TRACTOR
[FARMERHENFOXTRACTOR] => FARMER HEN FOX TRACTOR
[FARMERHENFOXTRACTORBARNYARD] => FARMER HEN FOX TRACTOR BARNYARD
[FARMERHENTRACTOR] => FARMER HEN TRACTOR
[FARMERHENTRACTORBARNYARD] => FARMER HEN TRACTOR BARNYARD
[FARMERHENTRACTORBARNYARDFOX] => FARMER HEN TRACTOR BARNYARD FOX
[FARMERHENTRACTORFOX] => FARMER HEN TRACTOR FOX
[FARMERHENTRACTORFOXBARNYARD] => FARMER HEN TRACTOR FOX BARNYARD
[FARMERTRACTOR] => FARMER TRACTOR
[FARMERTRACTORBARNYARD] => FARMER TRACTOR BARNYARD
[FARMERTRACTORBARNYARDFOX] => FARMER TRACTOR BARNYARD FOX
[FARMERTRACTORBARNYARDFOXHEN] => FARMER TRACTOR BARNYARD FOX HEN
[FARMERTRACTORBARNYARDHEN] => FARMER TRACTOR BARNYARD HEN
[FARMERTRACTORBARNYARDHENFOX] => FARMER TRACTOR BARNYARD HEN FOX
[FARMERTRACTORFOX] => FARMER TRACTOR FOX
[FARMERTRACTORFOXBARNYARD] => FARMER TRACTOR FOX BARNYARD
[FARMERTRACTORFOXBARNYARDHEN] => FARMER TRACTOR FOX BARNYARD HEN
[FARMERTRACTORFOXHEN] => FARMER TRACTOR FOX HEN
[FARMERTRACTORFOXHENBARNYARD] => FARMER TRACTOR FOX HEN BARNYARD
[FARMERTRACTORHEN] => FARMER TRACTOR HEN
[FARMERTRACTORHENBARNYARD] => FARMER TRACTOR HEN BARNYARD
[FARMERTRACTORHENBARNYARDFOX] => FARMER TRACTOR HEN BARNYARD FOX
[FARMERTRACTORHENFOX] => FARMER TRACTOR HEN FOX
[FARMERTRACTORHENFOXBARNYARD] => FARMER TRACTOR HEN FOX BARNYARD
[FOX] => FOX
[FOXBARNYARD] => FOX BARNYARD
[FOXBARNYARDFARMER] => FOX BARNYARD FARMER
[FOXBARNYARDFARMERHEN] => FOX BARNYARD FARMER HEN
[FOXBARNYARDFARMERHENTRACTOR] => FOX BARNYARD FARMER HEN TRACTOR
[FOXBARNYARDFARMERTRACTOR] => FOX BARNYARD FARMER TRACTOR
[FOXBARNYARDFARMERTRACTORHEN] => FOX BARNYARD FARMER TRACTOR HEN
[FOXBARNYARDHEN] => FOX BARNYARD HEN
[FOXBARNYARDHENFARMER] => FOX BARNYARD HEN FARMER
[FOXBARNYARDHENFARMERTRACTOR] => FOX BARNYARD HEN FARMER TRACTOR
[FOXBARNYARDHENTRACTOR] => FOX BARNYARD HEN TRACTOR
[FOXBARNYARDHENTRACTORFARMER] => FOX BARNYARD HEN TRACTOR FARMER
[FOXBARNYARDTRACTOR] => FOX BARNYARD TRACTOR
[FOXBARNYARDTRACTORFARMER] => FOX BARNYARD TRACTOR FARMER
[FOXBARNYARDTRACTORFARMERHEN] => FOX BARNYARD TRACTOR FARMER HEN
[FOXBARNYARDTRACTORHEN] => FOX BARNYARD TRACTOR HEN
[FOXBARNYARDTRACTORHENFARMER] => FOX BARNYARD TRACTOR HEN FARMER
[FOXFARMER] => FOX FARMER
[FOXFARMERBARNYARD] => FOX FARMER BARNYARD
[FOXFARMERBARNYARDHEN] => FOX FARMER BARNYARD HEN
[FOXFARMERBARNYARDHENTRACTOR] => FOX FARMER BARNYARD HEN TRACTOR
[FOXFARMERBARNYARDTRACTOR] => FOX FARMER BARNYARD TRACTOR
[FOXFARMERBARNYARDTRACTORHEN] => FOX FARMER BARNYARD TRACTOR HEN
[FOXFARMERHEN] => FOX FARMER HEN
[FOXFARMERHENBARNYARD] => FOX FARMER HEN BARNYARD
[FOXFARMERHENBARNYARDTRACTOR] => FOX FARMER HEN BARNYARD TRACTOR
[FOXFARMERHENTRACTOR] => FOX FARMER HEN TRACTOR
[FOXFARMERHENTRACTORBARNYARD] => FOX FARMER HEN TRACTOR BARNYARD
[FOXFARMERTRACTOR] => FOX FARMER TRACTOR
[FOXFARMERTRACTORBARNYARD] => FOX FARMER TRACTOR BARNYARD
[FOXFARMERTRACTORBARNYARDHEN] => FOX FARMER TRACTOR BARNYARD HEN
[FOXFARMERTRACTORHEN] => FOX FARMER TRACTOR HEN
[FOXFARMERTRACTORHENBARNYARD] => FOX FARMER TRACTOR HEN BARNYARD
[FOXHEN] => FOX HEN
[FOXHENBARNYARD] => FOX HEN BARNYARD
[FOXHENBARNYARDFARMER] => FOX HEN BARNYARD FARMER
[FOXHENBARNYARDFARMERTRACTOR] => FOX HEN BARNYARD FARMER TRACTOR
[FOXHENBARNYARDTRACTOR] => FOX HEN BARNYARD TRACTOR
[FOXHENBARNYARDTRACTORFARMER] => FOX HEN BARNYARD TRACTOR FARMER
[FOXHENFARMER] => FOX HEN FARMER
[FOXHENFARMERBARNYARD] => FOX HEN FARMER BARNYARD
[FOXHENFARMERBARNYARDTRACTOR] => FOX HEN FARMER BARNYARD TRACTOR
[FOXHENFARMERTRACTOR] => FOX HEN FARMER TRACTOR
[FOXHENFARMERTRACTORBARNYARD] => FOX HEN FARMER TRACTOR BARNYARD
[FOXHENTRACTOR] => FOX HEN TRACTOR
[FOXHENTRACTORBARNYARD] => FOX HEN TRACTOR BARNYARD
[FOXHENTRACTORBARNYARDFARMER] => FOX HEN TRACTOR BARNYARD FARMER
[FOXHENTRACTORFARMER] => FOX HEN TRACTOR FARMER
[FOXHENTRACTORFARMERBARNYARD] => FOX HEN TRACTOR FARMER BARNYARD
[FOXTRACTOR] => FOX TRACTOR
[FOXTRACTORBARNYARD] => FOX TRACTOR BARNYARD
[FOXTRACTORBARNYARDFARMER] => FOX TRACTOR BARNYARD FARMER
[FOXTRACTORBARNYARDFARMERHEN] => FOX TRACTOR BARNYARD FARMER HEN
[FOXTRACTORBARNYARDHEN] => FOX TRACTOR BARNYARD HEN
[FOXTRACTORBARNYARDHENFARMER] => FOX TRACTOR BARNYARD HEN FARMER
[FOXTRACTORFARMER] => FOX TRACTOR FARMER
[FOXTRACTORFARMERBARNYARD] => FOX TRACTOR FARMER BARNYARD
[FOXTRACTORFARMERBARNYARDHEN] => FOX TRACTOR FARMER BARNYARD HEN
[FOXTRACTORFARMERHEN] => FOX TRACTOR FARMER HEN
[FOXTRACTORFARMERHENBARNYARD] => FOX TRACTOR FARMER HEN BARNYARD
[FOXTRACTORHEN] => FOX TRACTOR HEN
[FOXTRACTORHENBARNYARD] => FOX TRACTOR HEN BARNYARD
[FOXTRACTORHENBARNYARDFARMER] => FOX TRACTOR HEN BARNYARD FARMER
[FOXTRACTORHENFARMER] => FOX TRACTOR HEN FARMER
[FOXTRACTORHENFARMERBARNYARD] => FOX TRACTOR HEN FARMER BARNYARD
[HEN] => HEN
[HENBARNYARD] => HEN BARNYARD
[HENBARNYARDFARMER] => HEN BARNYARD FARMER
[HENBARNYARDFARMERFOX] => HEN BARNYARD FARMER FOX
[HENBARNYARDFARMERFOXTRACTOR] => HEN BARNYARD FARMER FOX TRACTOR
[HENBARNYARDFARMERTRACTOR] => HEN BARNYARD FARMER TRACTOR
[HENBARNYARDFARMERTRACTORFOX] => HEN BARNYARD FARMER TRACTOR FOX
[HENBARNYARDFOX] => HEN BARNYARD FOX
[HENBARNYARDFOXFARMER] => HEN BARNYARD FOX FARMER
[HENBARNYARDFOXFARMERTRACTOR] => HEN BARNYARD FOX FARMER TRACTOR
[HENBARNYARDFOXTRACTOR] => HEN BARNYARD FOX TRACTOR
[HENBARNYARDFOXTRACTORFARMER] => HEN BARNYARD FOX TRACTOR FARMER
[HENBARNYARDTRACTOR] => HEN BARNYARD TRACTOR
[HENBARNYARDTRACTORFARMER] => HEN BARNYARD TRACTOR FARMER
[HENBARNYARDTRACTORFARMERFOX] => HEN BARNYARD TRACTOR FARMER FOX
[HENBARNYARDTRACTORFOX] => HEN BARNYARD TRACTOR FOX
[HENBARNYARDTRACTORFOXFARMER] => HEN BARNYARD TRACTOR FOX FARMER
[HENFARMER] => HEN FARMER
[HENFARMERBARNYARD] => HEN FARMER BARNYARD
[HENFARMERBARNYARDFOX] => HEN FARMER BARNYARD FOX
[HENFARMERBARNYARDFOXTRACTOR] => HEN FARMER BARNYARD FOX TRACTOR
[HENFARMERBARNYARDTRACTOR] => HEN FARMER BARNYARD TRACTOR
[HENFARMERBARNYARDTRACTORFOX] => HEN FARMER BARNYARD TRACTOR FOX
[HENFARMERFOX] => HEN FARMER FOX
[HENFARMERFOXBARNYARD] => HEN FARMER FOX BARNYARD
[HENFARMERFOXBARNYARDTRACTOR] => HEN FARMER FOX BARNYARD TRACTOR
[HENFARMERFOXTRACTOR] => HEN FARMER FOX TRACTOR
[HENFARMERFOXTRACTORBARNYARD] => HEN FARMER FOX TRACTOR BARNYARD
[HENFARMERTRACTOR] => HEN FARMER TRACTOR
[HENFARMERTRACTORBARNYARD] => HEN FARMER TRACTOR BARNYARD
[HENFARMERTRACTORBARNYARDFOX] => HEN FARMER TRACTOR BARNYARD FOX
[HENFARMERTRACTORFOX] => HEN FARMER TRACTOR FOX
[HENFARMERTRACTORFOXBARNYARD] => HEN FARMER TRACTOR FOX BARNYARD
[HENFOX] => HEN FOX
[HENFOXBARNYARD] => HEN FOX BARNYARD
[HENFOXBARNYARDFARMER] => HEN FOX BARNYARD FARMER
[HENFOXBARNYARDFARMERTRACTOR] => HEN FOX BARNYARD FARMER TRACTOR
[HENFOXBARNYARDTRACTOR] => HEN FOX BARNYARD TRACTOR
[HENFOXBARNYARDTRACTORFARMER] => HEN FOX BARNYARD TRACTOR FARMER
[HENFOXFARMER] => HEN FOX FARMER
[HENFOXFARMERBARNYARD] => HEN FOX FARMER BARNYARD
[HENFOXFARMERBARNYARDTRACTOR] => HEN FOX FARMER BARNYARD TRACTOR
[HENFOXFARMERTRACTOR] => HEN FOX FARMER TRACTOR
[HENFOXFARMERTRACTORBARNYARD] => HEN FOX FARMER TRACTOR BARNYARD
[HENFOXTRACTOR] => HEN FOX TRACTOR
[HENFOXTRACTORBARNYARD] => HEN FOX TRACTOR BARNYARD
[HENFOXTRACTORBARNYARDFARMER] => HEN FOX TRACTOR BARNYARD FARMER
[HENFOXTRACTORFARMER] => HEN FOX TRACTOR FARMER
[HENFOXTRACTORFARMERBARNYARD] => HEN FOX TRACTOR FARMER BARNYARD
[HENTRACTOR] => HEN TRACTOR
[HENTRACTORBARNYARD] => HEN TRACTOR BARNYARD
[HENTRACTORBARNYARDFARMER] => HEN TRACTOR BARNYARD FARMER
[HENTRACTORBARNYARDFARMERFOX] => HEN TRACTOR BARNYARD FARMER FOX
[HENTRACTORBARNYARDFOX] => HEN TRACTOR BARNYARD FOX
[HENTRACTORBARNYARDFOXFARMER] => HEN TRACTOR BARNYARD FOX FARMER
[HENTRACTORFARMER] => HEN TRACTOR FARMER
[HENTRACTORFARMERBARNYARD] => HEN TRACTOR FARMER BARNYARD
[HENTRACTORFARMERBARNYARDFOX] => HEN TRACTOR FARMER BARNYARD FOX
[HENTRACTORFARMERFOX] => HEN TRACTOR FARMER FOX
[HENTRACTORFARMERFOXBARNYARD] => HEN TRACTOR FARMER FOX BARNYARD
[HENTRACTORFOX] => HEN TRACTOR FOX
[HENTRACTORFOXBARNYARD] => HEN TRACTOR FOX BARNYARD
[HENTRACTORFOXBARNYARDFARMER] => HEN TRACTOR FOX BARNYARD FARMER
[HENTRACTORFOXFARMER] => HEN TRACTOR FOX FARMER
[HENTRACTORFOXFARMERBARNYARD] => HEN TRACTOR FOX FARMER BARNYARD
[TRACTOR] => TRACTOR
[TRACTORBARNYARD] => TRACTOR BARNYARD
[TRACTORBARNYARDFARMER] => TRACTOR BARNYARD FARMER
[TRACTORBARNYARDFARMERFOX] => TRACTOR BARNYARD FARMER FOX
[TRACTORBARNYARDFARMERFOXHEN] => TRACTOR BARNYARD FARMER FOX HEN
[TRACTORBARNYARDFARMERHEN] => TRACTOR BARNYARD FARMER HEN
[TRACTORBARNYARDFARMERHENFOX] => TRACTOR BARNYARD FARMER HEN FOX
[TRACTORBARNYARDFOX] => TRACTOR BARNYARD FOX
[TRACTORBARNYARDFOXFARMER] => TRACTOR BARNYARD FOX FARMER
[TRACTORBARNYARDFOXFARMERHEN] => TRACTOR BARNYARD FOX FARMER HEN
[TRACTORBARNYARDFOXHEN] => TRACTOR BARNYARD FOX HEN
[TRACTORBARNYARDFOXHENFARMER] => TRACTOR BARNYARD FOX HEN FARMER
[TRACTORBARNYARDHEN] => TRACTOR BARNYARD HEN
[TRACTORBARNYARDHENFARMER] => TRACTOR BARNYARD HEN FARMER
[TRACTORBARNYARDHENFARMERFOX] => TRACTOR BARNYARD HEN FARMER FOX
[TRACTORBARNYARDHENFOX] => TRACTOR BARNYARD HEN FOX
[TRACTORBARNYARDHENFOXFARMER] => TRACTOR BARNYARD HEN FOX FARMER
[TRACTORFARMER] => TRACTOR FARMER
[TRACTORFARMERBARNYARD] => TRACTOR FARMER BARNYARD
[TRACTORFARMERBARNYARDFOX] => TRACTOR FARMER BARNYARD FOX
[TRACTORFARMERBARNYARDFOXHEN] => TRACTOR FARMER BARNYARD FOX HEN
[TRACTORFARMERBARNYARDHEN] => TRACTOR FARMER BARNYARD HEN
[TRACTORFARMERBARNYARDHENFOX] => TRACTOR FARMER BARNYARD HEN FOX
[TRACTORFARMERFOX] => TRACTOR FARMER FOX
[TRACTORFARMERFOXBARNYARD] => TRACTOR FARMER FOX BARNYARD
[TRACTORFARMERFOXBARNYARDHEN] => TRACTOR FARMER FOX BARNYARD HEN
[TRACTORFARMERFOXHEN] => TRACTOR FARMER FOX HEN
[TRACTORFARMERFOXHENBARNYARD] => TRACTOR FARMER FOX HEN BARNYARD
[TRACTORFARMERHEN] => TRACTOR FARMER HEN
[TRACTORFARMERHENBARNYARD] => TRACTOR FARMER HEN BARNYARD
[TRACTORFARMERHENBARNYARDFOX] => TRACTOR FARMER HEN BARNYARD FOX
[TRACTORFARMERHENFOX] => TRACTOR FARMER HEN FOX
[TRACTORFARMERHENFOXBARNYARD] => TRACTOR FARMER HEN FOX BARNYARD
[TRACTORFOX] => TRACTOR FOX
[TRACTORFOXBARNYARD] => TRACTOR FOX BARNYARD
[TRACTORFOXBARNYARDFARMER] => TRACTOR FOX BARNYARD FARMER
[TRACTORFOXBARNYARDFARMERHEN] => TRACTOR FOX BARNYARD FARMER HEN
[TRACTORFOXBARNYARDHEN] => TRACTOR FOX BARNYARD HEN
[TRACTORFOXBARNYARDHENFARMER] => TRACTOR FOX BARNYARD HEN FARMER
[TRACTORFOXFARMER] => TRACTOR FOX FARMER
[TRACTORFOXFARMERBARNYARD] => TRACTOR FOX FARMER BARNYARD
[TRACTORFOXFARMERBARNYARDHEN] => TRACTOR FOX FARMER BARNYARD HEN
[TRACTORFOXFARMERHEN] => TRACTOR FOX FARMER HEN
[TRACTORFOXFARMERHENBARNYARD] => TRACTOR FOX FARMER HEN BARNYARD
[TRACTORFOXHEN] => TRACTOR FOX HEN
[TRACTORFOXHENBARNYARD] => TRACTOR FOX HEN BARNYARD
[TRACTORFOXHENBARNYARDFARMER] => TRACTOR FOX HEN BARNYARD FARMER
[TRACTORFOXHENFARMER] => TRACTOR FOX HEN FARMER
[TRACTORFOXHENFARMERBARNYARD] => TRACTOR FOX HEN FARMER BARNYARD
[TRACTORHEN] => TRACTOR HEN
[TRACTORHENBARNYARD] => TRACTOR HEN BARNYARD
[TRACTORHENBARNYARDFARMER] => TRACTOR HEN BARNYARD FARMER
[TRACTORHENBARNYARDFARMERFOX] => TRACTOR HEN BARNYARD FARMER FOX
[TRACTORHENBARNYARDFOX] => TRACTOR HEN BARNYARD FOX
[TRACTORHENBARNYARDFOXFARMER] => TRACTOR HEN BARNYARD FOX FARMER
[TRACTORHENFARMER] => TRACTOR HEN FARMER
[TRACTORHENFARMERBARNYARD] => TRACTOR HEN FARMER BARNYARD
[TRACTORHENFARMERBARNYARDFOX] => TRACTOR HEN FARMER BARNYARD FOX
[TRACTORHENFARMERFOX] => TRACTOR HEN FARMER FOX
[TRACTORHENFARMERFOXBARNYARD] => TRACTOR HEN FARMER FOX BARNYARD
[TRACTORHENFOX] => TRACTOR HEN FOX
[TRACTORHENFOXBARNYARD] => TRACTOR HEN FOX BARNYARD
[TRACTORHENFOXBARNYARDFARMER] => TRACTOR HEN FOX BARNYARD FARMER
[TRACTORHENFOXFARMER] => TRACTOR HEN FOX FARMER
[TRACTORHENFOXFARMERBARNYARD] => TRACTOR HEN FOX FARMER BARNYARD
)
答案 1 :(得分:0)
$str_array = explode(','. $string);
//sort($str_array); // or ksort($str_array);
// if you need the text as array_keys, try in combination with array_flip()
foreach ($str_array AS $single_str) {
echo trim($single_str).'<br />';
}
答案 2 :(得分:0)
$chars=explode(",",$string);
有关其他问题,请参阅此solution。这可以使用全局$ arr进行修改。 $ chars数组的分配应该是global $chars;
空间必须保留。现在取echo $uj.'<br />';
并更改为$strl=str_replace(" ","",$uj); $strk=substr($uj,0,strlen($uj)-1); $str[$strl]=$strk;
P.S。我知道全球不好,但这是最简单的方法。
答案 3 :(得分:0)
**This is the exact result for your question**
<?php
$strdata = "BARNYARD,FARMER,FOX,HEN,TRACTOR";
$arrayData =explode(',',$strdata);
foreach($arrayData as $key=>$value):
$strpvalue = str_replace(" ","",$value);
$str[$strpvalue] = $arrayData[$key];
endforeach;
//1 set
$str[$str[$arrayData[0]].$arrayData[1]] = $arrayData[0]." ".$arrayData[1];
$str[$str[$arrayData[0]].$arrayData[2]] = $arrayData[0]." ".$arrayData[2];
$str[$str[$arrayData[0]].$arrayData[3]] = $arrayData[0]." ".$arrayData[3];
$str[$str[$arrayData[0]].$arrayData[4]] = $arrayData[0]." ".$arrayData[4];
//2 set
$str[$str[$arrayData[1]].$arrayData[2]] = $arrayData[1]." ".$arrayData[2];
$str[$str[$arrayData[1]].$arrayData[3]] = $arrayData[1]." ".$arrayData[3];
$str[$str[$arrayData[1]].$arrayData[4]] = $arrayData[1]." ".$arrayData[4];
//3 set
$str[$str[$arrayData[2]].$arrayData[3]] = $arrayData[2]." ".$arrayData[3];
$str[$str[$arrayData[2]].$arrayData[4]] = $arrayData[2]." ".$arrayData[4];
//4 set
$str[$str[$arrayData[3]].$arrayData[4]] = $arrayData[3]." ".$arrayData[4];
//5 set
$str[$str[$arrayData[0]].$arrayData[1].$arrayData[2]] = $arrayData[0]." ".$arrayData[1]." ".$arrayData[2];
$str[$str[$arrayData[0]].$arrayData[2].$arrayData[3]] = $arrayData[0]." ".$arrayData[2]." ".$arrayData[3];
$str[$str[$arrayData[0]].$arrayData[3].$arrayData[4]] = $arrayData[0]." ".$arrayData[3]." ".$arrayData[4];
//6 set
$str[$str[$arrayData[1]].$arrayData[2].$arrayData[3]] = $arrayData[1]." ".$arrayData[2]." ".$arrayData[3];
$str[$str[$arrayData[1]].$arrayData[3].$arrayData[4]] = $arrayData[1]." ".$arrayData[3]." ".$arrayData[4];
//7 set
$str[$str[$arrayData[2]].$arrayData[3].$arrayData[4]] = $arrayData[2]." ".$arrayData[3]." ".$arrayData[4];
//8 set
$str[$str[$arrayData[0]].$arrayData[1].$arrayData[2].$arrayData[3]] = $arrayData[0]." ".$arrayData[1]." ".$arrayData[2]." ".$arrayData[3];
$str[$str[$arrayData[0]].$arrayData[2].$arrayData[3].$arrayData[4]] = $arrayData[0]." ".$arrayData[2]." ".$arrayData[3]." ".$arrayData[4];
//9 set
$str[$str[$arrayData[1]].$arrayData[2].$arrayData[3].$arrayData[4]] = $arrayData[1]." ".$arrayData[2]." ".$arrayData[3]." ".$arrayData[4];
//10 set
$str[$str[$arrayData[0]].$arrayData[1].$arrayData[2].$arrayData[3].$arrayData[4]] = $arrayData[0]." ".$arrayData[1]." ".$arrayData[2]." ".$arrayData[3]." ".$arrayData[4];
asort($str);
echo "<pre>";
print_r($str);
echo "</pre>";
?>
答案 4 :(得分:0)
如果使用PEAR是一个选项,您会发现Math_Combinatorics类很有用:
<?php
$input = array("BARNYARD", "FARMER", "FOX", "HEN", "TRACTOR");
$words = array();
require_once("Math/Combinatorics.php");
$combinatorics = new Math_Combinatorics;
for($i = 1; $i <= count($input); $i++) {
$combinations = $combinatorics->combinations($input, $i);
foreach($combinations as $combination) {
$key = implode("", $combination);
$val = implode(" ", $combination);
$words[$key] = $val;
}
}
print_r($words);
Array
(
[BARNYARD] => BARNYARD
[FARMER] => FARMER
[FOX] => FOX
[HEN] => HEN
[TRACTOR] => TRACTOR
[BARNYARDFARMER] => BARNYARD FARMER
[BARNYARDFOX] => BARNYARD FOX
[BARNYARDHEN] => BARNYARD HEN
[BARNYARDTRACTOR] => BARNYARD TRACTOR
[FARMERFOX] => FARMER FOX
[FARMERHEN] => FARMER HEN
[FARMERTRACTOR] => FARMER TRACTOR
[FOXHEN] => FOX HEN
[FOXTRACTOR] => FOX TRACTOR
[HENTRACTOR] => HEN TRACTOR
[BARNYARDFARMERFOX] => BARNYARD FARMER FOX
[BARNYARDFARMERHEN] => BARNYARD FARMER HEN
[BARNYARDFARMERTRACTOR] => BARNYARD FARMER TRACTOR
[BARNYARDFOXHEN] => BARNYARD FOX HEN
[BARNYARDFOXTRACTOR] => BARNYARD FOX TRACTOR
[BARNYARDHENTRACTOR] => BARNYARD HEN TRACTOR
[FARMERFOXHEN] => FARMER FOX HEN
[FARMERFOXTRACTOR] => FARMER FOX TRACTOR
[FARMERHENTRACTOR] => FARMER HEN TRACTOR
[FOXHENTRACTOR] => FOX HEN TRACTOR
[BARNYARDFARMERFOXHEN] => BARNYARD FARMER FOX HEN
[BARNYARDFARMERFOXTRACTOR] => BARNYARD FARMER FOX TRACTOR
[BARNYARDFARMERHENTRACTOR] => BARNYARD FARMER HEN TRACTOR
[BARNYARDFOXHENTRACTOR] => BARNYARD FOX HEN TRACTOR
[FARMERFOXHENTRACTOR] => FARMER FOX HEN TRACTOR
[BARNYARDFARMERFOXHENTRACTOR] => BARNYARD FARMER FOX HEN TRACTOR
)