开发者

Is there something wrong with this foreach code?

开发者 https://www.devze.com 2023-01-14 02:41 出处:网络
foreach ($data[\'tests\'] as $testname => $tests) { echo \"<h1>Extraction $testname Tests</h1>\\n\";
foreach ($data['tests'] as $testname => $tests) {
echo "<h1>Extraction $testname Tests</h1>\n";
$function = $testfunctions[$testname];

echo "<ul>";
foreach ($tests as $test) {
    echo "<li>" . $test['description'] . ' ... ';
    $extracted = $extractor->$function($test['text']);
    if ($test['expected'] =开发者_运维问答= $extracted) {
        echo " <span style='color: green'>passed.</span></li>";
    } else {
        echo " <span style='color: red'>failed.</span>";
        echo "<pre>Original: " . htmlspecialchars($test['text']) . "\nExpected: " . print_r($test['expected'], true) . "\nActual  : " . print_r($extracted, true) . "</pre>";
    }
    echo "</li>";
}
echo "</ul>";}

I keep getting the error:

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\test\runtests.php on line 49

p.s. the beginning of the code is line 49, so the probelm starts with the foreach statment.


Whenever i see that, it tends to mean that the thing i'm trying to iterate through isn't an array.

Check $data['tests'] (and each inner $tests) to make sure it's not null/unset/empty, and that it's something iterable like an array. Also keep in mind that older versions of PHP (before 5.0?) don't do iterable objects very well.


One of the elements in $data["tests"] is probably not an array.

Add this before the foreach:

if (is_array($tests))
 foreach ($tests as $test) {...
0

精彩评论

暂无评论...
验证码 换一张
取 消