先进的多维度难度

时间:2017-05-17 21:39:24

标签: php arrays

我试图回应一个阵列,但是我得到的错误我无法弄明白。 尝试改变代码的sereval方法,我认为到目前为止我的最佳输出是:

$b  = array(
    array("number 1",   array(0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0)),
    array("number 2",   array(1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1)),
    array("number 3",   array(0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1)),
    array("number 4",   array(1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0)),
    array("number 5",   array(0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0)),
    array("number 6",   array(0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0)),
    array("number 7",   array(1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1)),
    array("number 8",   array(0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)),
    array("number 9",   array(0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1)),
);

for ($row=0; $row < count($b); $row++) { 
    echo "<p><b>Answer : </b>".$b[$row][0]."</p>";
    for ($col=0; $col < 30; $col++) {
        echo "<p>".$b[$row][$col]."</p></br>";
    }
}

这给了我以下错误:

  

number 1 number 1注意:第62行的C:\ xampp \ htdocs \ vendoTest \ test.php中的数组到字符串转换数组注意:未定义的偏移量:C:\ xampp \ htdocs \ vendoTest \ test.php中的2第62行

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

<?php

$b  = array(
    array("number 1",   array(0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0)),
    array("number 2",   array(1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1)),
    array("number 3",   array(0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1)),
    array("number 4",   array(1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0)),
    array("number 5",   array(0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0)),
    array("number 6",   array(0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0)),
    array("number 7",   array(1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1)),
    array("number 8",   array(0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)),
    array("number 9",   array(0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1)),
);

foreach ( $b as $answers )
{
    echo "<p><b>Answer : </b>".$answers[0]."</p>";
    foreach ( $answers[1] as $val )
    {
        echo "<p>".$val."</p></br>";
    }
}

我相信这就是你想要的。有关输出,请参见this playground。

您的代码的主要问题源于此行:

echo "<p>".$b[$row][$col]."</p></br>";

$b[$row]的一个例子就是:

array("number 1",   array(0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0))

索引0是答案,索引1是0&1和1的实际数组。因此,如果要循环遍历第二个数组,则需要使用$b[$row][1][$col]

您的固定代码:

for ($row=0; $row < count($b); $row++) { 
    echo "<p><b>Answer : </b>".$b[$row][0]."</p>";
    for ($col=0; $col < 30; $col++) {
        echo "<p>".$b[$row][1][$col]."</p></br>";
    }
}