簡體   English   中英

這個foreach代碼有什么問題嗎?

[英]Is there something wrong with this foreach code?

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>";}

我不斷收到錯誤:

警告:第49行的C:\\ xampp \\ htdocs \\ test \\ runtests.php中為foreach()提供了無效的參數

ps代碼的開頭是第49行,因此該探針以foreach語句開頭。

每當我看到它時,它往往意味着我要遍歷的東西不是數組。

檢查$data['tests'] (以及每個內部的$tests )以確保它不是null / unset / empty,並且像數組一樣可迭代。 還請記住,舊版本的PHP(5.0之前的版本)不能很好地處理可迭代對象。

$data["tests"]中的元素之一可能不是數組。

在foreach之前添加:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM