2013-11-22 66 views
-5

我有一個腳本,找到多維數組的所有可能的組合:注意:數組字符串轉換錯誤

我得到這個錯誤:

<?php 
    $traits = array 
    (
     array('Happy', 'Sad', 'Angry', 'Hopeful'), 
     array('Outgoing', 'Introverted'), 
     array('Tall', 'Short', 'Medium'), 
     array('Handsome', 'Plain', 'Ugly') 
    ); 

    $i = 0; 
    if ($i >= count($traits)) 
     echo "<BR>\n"; 
    else 
    { 
     foreach ($traits[$i] as $trait) 
     { 
      foreach($traits as $item) 
      { 
       echo '<BR>'.$trait, $item, $i + 1; 
      } 
     } 
    } 
?> 

在哪裏我會錯呢?

這是原代碼:

<?php 
    function showCombinations($string, $traits, $i) 
    { 
     if ($i >= count($traits)) 
      echo trim($string) . "\n"; 
     else 
     { 
      foreach ($traits[$i] as $trait) 
       showCombinations("$string $trait", $traits, $i + 1); 
     } 
    } 

    $traits = array 
    (
     array('Happy', 'Sad', 'Angry', 'Hopeful'), 
     array('Outgoing', 'Introverted'), 
     array('Tall', 'Short', 'Medium'), 
     array('Handsome', 'Plain', 'Ugly') 
    ); 

    showCombinations('', $traits, 0); 
?> 

所以我刪除的功能,因爲我不想使用的功能。從第1碼

預期輸出:

Happy Outgoing Tall Handsome 
Happy Outgoing Tall Plain 
Happy Outgoing Tall Ugly 
Happy Outgoing Short Handsome 
Happy Outgoing Short Plain 
Happy Outgoing Short Ugly 
Happy Outgoing Medium Handsome 
Happy Outgoing Medium Plain 
Happy Outgoing Medium Ugly 
Happy Introverted Tall Handsome 
Happy Introverted Tall Plain 
Happy Introverted Tall Ugly 
Happy Introverted Short Handsome 
Happy Introverted Short Plain 
Happy Introverted Short Ugly 
Happy Introverted Medium Handsome 
Happy Introverted Medium Plain 
Happy Introverted Medium Ugly 
Sad Outgoing Tall Handsome 
Sad Outgoing Tall Plain 
Sad Outgoing Tall Ugly 
Sad Outgoing Short Handsome 
Sad Outgoing Short Plain 
Sad Outgoing Short Ugly 
Sad Outgoing Medium Handsome 
Sad Outgoing Medium Plain 
Sad Outgoing Medium Ugly 
Sad Introverted Tall Handsome 
Sad Introverted Tall Plain 
Sad Introverted Tall Ugly 
Sad Introverted Short Handsome 
Sad Introverted Short Plain 
Sad Introverted Short Ugly 
Sad Introverted Medium Handsome 
Sad Introverted Medium Plain 
Sad Introverted Medium Ugly 
Angry Outgoing Tall Handsome 
Angry Outgoing Tall Plain 
Angry Outgoing Tall Ugly 
Angry Outgoing Short Handsome 
Angry Outgoing Short Plain 
Angry Outgoing Short Ugly 
Angry Outgoing Medium Handsome 
Angry Outgoing Medium Plain 
Angry Outgoing Medium Ugly 
Angry Introverted Tall Handsome 
Angry Introverted Tall Plain 
Angry Introverted Tall Ugly 
Angry Introverted Short Handsome 
Angry Introverted Short Plain 
Angry Introverted Short Ugly 
Angry Introverted Medium Handsome 
Angry Introverted Medium Plain 
Angry Introverted Medium Ugly 
Hopeful Outgoing Tall Handsome 
Hopeful Outgoing Tall Plain 
Hopeful Outgoing Tall Ugly 
Hopeful Outgoing Short Handsome 
Hopeful Outgoing Short Plain 
Hopeful Outgoing Short Ugly 
Hopeful Outgoing Medium Handsome 
Hopeful Outgoing Medium Plain 
Hopeful Outgoing Medium Ugly 
Hopeful Introverted Tall Handsome 
Hopeful Introverted Tall Plain 
Hopeful Introverted Tall Ugly 
Hopeful Introverted Short Handsome 
Hopeful Introverted Short Plain 
Hopeful Introverted Short Ugly 
Hopeful Introverted Medium Handsome 
Hopeful Introverted Medium Plain 
Hopeful Introverted Medium Ugly 
+2

如果你陳述你想要完成的事情,你可能會得到更好的答案。 –

+0

Upvoted問題並投票重新編輯後重新打開。 – andyb

回答

1

首先,你需要改變:

foreach ($traits[$i] as $trait) 

要:

foreach ($traits as $trait) 

然後意識到$trait仍然是一個數組。因此,而不是

echo '<BR>'.$trait, $test, $i + 1; 

你要還是遍歷數組:

foreach($trait AS $value) 

現在你可以重複你的$value

foreach ($traits as $trait) { 
    foreach($trait AS $value) { 
     echo $value; 
    } 
} 

更新:下面的評論之間和幾個問題的更新,它變得清楚什麼期望的輸出實際上。最終的解決方案在這裏:http://3v4l.org/X3i3p

+1

他爲什麼要這樣? '$ traits'是一個二維數組,但'$ traits'不是。所以改變它仍然會返回以前的錯誤。 –

+0

@Darline你的代碼中仍然有'echo'
'。$ trait'。 '$ trait'是一個數組,你不能只是迴應它。 – jszobody

+0

@Darline看看我最後的'foreach'循環。這會讓你迴應每一個單獨的價值。你還需要做什麼? – jszobody

相關問題