2010-04-19 42 views
2

我有一組數據,看起來像這樣:如何使用PHP從數組創建子串?

2008, Honda, Accord, Used, Car 

而且我試圖找出一種方法,使若干子字符串從陣列中的每個項目。例如,我想循環的每個項目,並創建以下字符串:

2008 
2008 Honda 
2008 Accord 
2008 Used 
2008 Car 
2008 Honda Accord 
2008 Honda Used 
2008 Honda Car 
2008 Accord Honda 
2008 Accord Used 
2008 Accord Car 
2008 Used Honda 
2008 Used Accord 
2008 Used Car 
2008 Car Honda 
2008 Car Accord 
2008 Car Used 
Honda 
Honda 2008 
Honda Accord 
Honda Used 
Honda Car 
Honda 2008 Accord 
Honda 2008 Used 
etc ... 

我需要確保不存在創建重複項目,我需要防止它添加相同的單詞兩次(例如:本田本田或2008年本田2008年 - 我不想那樣)。有沒有人寫過這樣的東西,或者知道我在哪裏可以找到一個同樣的腳本?

+0

*(相關)* http://stackoverflow.com/questions/2246493/concatenate-值-n-array-in-php – Gordon 2010-04-19 21:11:31

+0

http://stackoverflow.com/questions/1679605/combinations-dispositions-and-permutations-in-php – outis 2010-04-19 22:10:32

回答

2

如何:

<?php 

$vals = array('2008', 'Honda', 'Accord', 'Used', 'Car'); 

function make_keyword_strings($array, $maxdepth = false, $prefix = '') { 
    $ret = array(); 
    // if we are tracking depths, subtract one from the depth: 
    $newdepth = $maxdepth === false ? false : $maxdepth-1; 
    if ($newdepth < 0) return $ret; // we went over our depth 
    foreach($array as $key => $value) { 
    $temp = $array; 
    unset($temp[$key]); // remove the current key 

    // add our current value ($prefix.$value), and recursively call the function with 
    // a new prefix. 
    $ret = array_merge($ret, 
      array(trim($prefix.$value)), 
      (array) make_keyword_strings($temp, $newdepth, $prefix.$value.' ')); 
    } 
    return $ret; 
} 

echo(implode("\n",make_keyword_strings($vals,3))); 

----結果輸出----

2008 
2008 Honda 
2008 Honda Accord 
2008 Honda Used 
2008 Honda Car 
2008 Accord 
2008 Accord Honda 
2008 Accord Used 
2008 Accord Car 
2008 Used 
2008 Used Honda 
2008 Used Accord 
2008 Used Car 
2008 Car 
2008 Car Honda 
2008 Car Accord 
2008 Car Used 
Honda 
Honda 2008 
Honda 2008 Accord 
Honda 2008 Used 
Honda 2008 Car 
Honda Accord 
Honda Accord 2008 
Honda Accord Used 
Honda Accord Car 
Honda Used 
Honda Used 2008 
Honda Used Accord 
Honda Used Car 
Honda Car 
Honda Car 2008 
Honda Car Accord 
Honda Car Used 
Accord 
Accord 2008 
Accord 2008 Honda 
Accord 2008 Used 
Accord 2008 Car 
Accord Honda 
Accord Honda 2008 
Accord Honda Used 
Accord Honda Car 
Accord Used 
Accord Used 2008 
Accord Used Honda 
Accord Used Car 
Accord Car 
Accord Car 2008 
Accord Car Honda 
Accord Car Used 
Used 
Used 2008 
Used 2008 Honda 
Used 2008 Accord 
Used 2008 Car 
Used Honda 
Used Honda 2008 
Used Honda Accord 
Used Honda Car 
Used Accord 
Used Accord 2008 
Used Accord Honda 
Used Accord Car 
Used Car 
Used Car 2008 
Used Car Honda 
Used Car Accord 
Car 
Car 2008 
Car 2008 Honda 
Car 2008 Accord 
Car 2008 Used 
Car Honda 
Car Honda 2008 
Car Honda Accord 
Car Honda Used 
Car Accord 
Car Accord 2008 
Car Accord Honda 
Car Accord Used 
Car Used 
Car Used 2008 
Car Used Honda 
Car Used Accord 
2

您可以聲明像{0,0,0,0,1}這樣的二進制數組X. 並在二進制邏輯中增加。在每次增加之後,您將打印一條生成的此規則字符串。如果在數組X上有字1,則表示字i存在,如果0字不存在。

您可以使用它的功能:

string decbin ( int $number ) 

她的文檔,你可以找到在:當你到達陣列由唯一的充滿 http://pl2.php.net/manual/en/function.decbin.php

你的算法停止。 在十進制表示是:

pow(2, sizeof(X)) - 1 
+0

非常好的主意。謝謝! – mike 2010-04-19 21:27:23

0

你想要做的是:

  1. 創建一個數組L
  2. 對於原始文本
    • 每個單詞如果單詞不是L不已,推字L

然後,它的生成您所需要的類型的所有可能的排列一件簡單的事情。