2014-11-21 72 views
2

我想找到一個方法來獲得這個工作。但我很難想出邏輯。php多維數組路徑段組合循環

我有此數組:

Array 
(
    [0] => Array 
     (
      [0] => news 
      [1] => {section} 
      [2] => {slug} 
      [3] => {*} 
     ) 

    [1] => Array 
     (
      [0] => {id} 
      [1] => {*} 
     ) 

    [2] => Array 
     (
      [0] => {date} 
      [1] => 25-07-1982 
      [2] => {section} 
      [3] => {slug} 
      [4] => {*} 
     ) 

) 

,我需要轉換爲這樣的結果:

0 news/{id}/{date} 
1 news/{id}/25-07-1982 
2 news/{id}/{section} 
3 news/{id}/{slug} 
4 news/{id}/{*} 
5 news/{*}/{date} 
6 news/{*}/25-07-1982 
7 news/{*}/{section} 
8 news/{*}/{slug} 
9 news/{*}/{*} 
10 {section}/{id}/{date} 
11 {section}/{id}/25-07-1982 
12 {section}/{id}/{section} 
13 {section}/{id}/{slug} 
14 {section}/{id}/{*} 
15 {section}/{*}/{date} 
16 {section}/{*}/25-07-1982 
17 {section}/{*}/{section} 
18 {section}/{*}/{slug} 
19 {section}/{*}/{*} 
20 {slug}/{id}/{date} 
21 {slug}/{id}/25-07-1982 
22 {slug}/{id}/{section} 
23 {slug}/{id}/{slug} 
24 {slug}/{id}/{*} 
25 {slug}/{*}/{date} 
26 {slug}/{*}/25-07-1982 
27 {slug}/{*}/{section} 
28 {slug}/{*}/{slug} 
29 {slug}/{*}/{*} 
30 {*}/{id}/{date} 
31 {*}/{id}/25-07-1982 
32 {*}/{id}/{section} 
33 {*}/{id}/{slug} 
34 {*}/{id}/{*} 
35 {*}/{*}/{date} 
36 {*}/{*}/25-07-1982 
37 {*}/{*}/{section} 
38 {*}/{*}/{slug} 
39 {*}/{*}/{*} 

輸入數組可以包含三個以上的按鍵,所以我在尋找解決方案應該是動態的。結果應該與上面顯示的結果具有相同的順序。 有人知道如何以有效的方式做到這一點?有人能給我一個正確的方向嗎?非常感謝! :)

+0

把「permutations php」放到http://google.com – zerkms 2014-11-21 08:42:12

+0

我認爲你需要的是3個嵌套循環 – Serpes 2014-11-21 08:44:51

+0

@Serpes是的,我現在看到它,謝謝! – 2014-11-21 08:52:48

回答

3

某事像這樣

foreach ($array[0] as $val0) 
    foreach ($array[1] as $val1) 
    foreach ($array[2] as $val2) 
     $newArray[] = "$val0/$val1/$val2"; 

編輯:變量數組長度

function recursive($array , $length = 0){ 
    $retval =array(); 

    if($length < count($array) -1){ 
    foreach ($array[$length] as $val0) 
     foreach (recursive($array, $length+1) as $val1) 
      $retval[] = "$val0/$val1"; 
    } 
    else 
    { 
    foreach ($array[$length] as $val0) 
     $retval[] = "$val0"; 
    } 

    return $retval; 
} 

print_r(recursive($array)); 
+0

噢,那實際上讓我在那裏!盯着這一個小時,但看不到這個:S。我認爲週末的時間...輸入數組也可以多於三個,但我認爲通過一些遞歸,我應該能夠得到這個工作! – 2014-11-21 08:51:43

+0

嗯,我不能做這個遞歸。有任何想法嗎? – 2014-11-21 09:43:24

+0

我加了遞歸函數 – 2014-11-21 13:08:09

0

只是因爲喜歡寫一個MIS /管理PHP陣列功能,我把這個在一起,主要是因爲我很確定你可以避免遞歸—,因爲結構本身不是遞歸的。 (我的頭似乎認爲這是一個規則,我敢肯定有人在某個地方可以證明它是錯的)

foreach (array_reverse($array) as $sub) { 
    if (isset($rem)) { 
    $ret = array(); 
    foreach ($sub as $itm) { 
     foreach ($rem as $val) { $ret[] = "$itm/$val"; } 
    } 
    $rem = $ret; 
    } 
    else { 
    $rem = $sub; 
    } 
} 

$rem中發現的輸出如下:

Array (
    [0] => news/{id}/{date} 
    [1] => news/{id}/25-07-1982 
    [2] => news/{id}/{section} 
    [3] => news/{id}/{slug} 
    [4] => news/{id}/{*} 
    [5] => news/{*}/{date} 
    [6] => news/{*}/25-07-1982 
    [7] => news/{*}/{section} 
    [8] => news/{*}/{slug} 
    [9] => news/{*}/{*} 
    [10] => {section}/{id}/{date} 
    [11] => {section}/{id}/25-07-1982 
    [12] => {section}/{id}/{section} 
    [13] => {section}/{id}/{slug} 
    [14] => {section}/{id}/{*} 
    [15] => {section}/{*}/{date} 
    [16] => {section}/{*}/25-07-1982 
    [17] => {section}/{*}/{section} 
    [18] => {section}/{*}/{slug} 
    [19] => {section}/{*}/{*} 
    [20] => {slug}/{id}/{date} 
    [21] => {slug}/{id}/25-07-1982 
    [22] => {slug}/{id}/{section} 
    [23] => {slug}/{id}/{slug} 
    [24] => {slug}/{id}/{*} 
    [25] => {slug}/{*}/{date} 
    [26] => {slug}/{*}/25-07-1982 
    [27] => {slug}/{*}/{section} 
    [28] => {slug}/{*}/{slug} 
    [29] => {slug}/{*}/{*} 
    [30] => {*}/{id}/{date} 
    [31] => {*}/{id}/25-07-1982 
    [32] => {*}/{id}/{section} 
    [33] => {*}/{id}/{slug} 
    [34] => {*}/{id}/{*} 
    [35] => {*}/{*}/{date} 
    [36] => {*}/{*}/25-07-1982 
    [37] => {*}/{*}/{section} 
    [38] => {*}/{*}/{slug} 
    [39] => {*}/{*}/{*} 
) 

此外,對於那些喜歡他們的多維數組,這可能會派上用場(雖然我不願意想的開銷是什麼對於這樣的代碼高爾夫版本)。需要說明的是,這第二個例子並不像OP所要求的那樣創建字符串列表,而是創建了一個層次數組結構。

foreach (array_reverse($array) as $sub) { 
    $rem = isset($rem) 
    ? array_combine($sub, array_fill(0, count($sub), $rem)) 
    : $sub 
    ; 
} 

這生成(再次$rem):

Array (
    [news] => Array (
     [{id}] => Array (
      [0] => {date} 
      [1] => 25-07-1982 
      [2] => {section} 
      [3] => {slug} 
      [4] => {*} 
     ) 
     [{*}] => Array (
      [0] => {date} 
      [1] => 25-07-1982 
      [2] => {section} 
      [3] => {slug} 
      [4] => {*} 
    ) 
    ) 

    [{section}] => Array (
     [{id}] => Array (
      [0] => {date} 
      [1] => 25-07-1982 
      [2] => {section} 
      [3] => {slug} 
      [4] => {*} 
     ) 

... and so on 

現在只要PHP有一個join_recursive,其中包括鍵。
(除了幫助上述以外,這幾乎毫無意義)

+0

哦!太好了,我現在只看到了這一點。看看我是否可以把它用在一些很好的用途上。謝謝! – 2014-11-26 08:04:45