注意:数组到字符串转换ERROR

时间:2013-11-22 13:43:34

标签: php arrays

我有一个脚本,可以找到多维数组的所有可能组合:

我收到了这个错误:

<?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);
?>

所以我删除了函数,因为我不想使用函数。

第一个代码的预期输出:

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

1 个答案:

答案 0 :(得分: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