我有一個腳本,找到多維數組的所有可能的組合:注意:數組字符串轉換錯誤
我得到這個錯誤:
<?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
如果你陳述你想要完成的事情,你可能會得到更好的答案。 –
Upvoted問題並投票重新編輯後重新打開。 – andyb