Php 如何抑制foreach()警告
我需要帮助抑制从Php 如何抑制foreach()警告,php,wordpress,loops,for-loop,foreach,Php,Wordpress,Loops,For Loop,Foreach,我需要帮助抑制从foreach()收到的警告。 我正在得到我想要的结果。我只需要删除警告 这是我的代码: for($i = 1; $i <= $RESULTS_count; $i++){ $rawResult = $wpdb->get_row( $wpdb->prepare("SELECT * FROM ".WPSQT_TABLE_RESULTS." WHERE id = $i"),ARRAY_A); $rawResult['sections
foreach()收到的警告。
我正在得到我想要的结果。我只需要删除警告
这是我的代码:
for($i = 1; $i <= $RESULTS_count; $i++){
$rawResult = $wpdb->get_row(
$wpdb->prepare("SELECT * FROM ".WPSQT_TABLE_RESULTS." WHERE id = $i"),ARRAY_A);
$rawResult['sections'] = unserialize($rawResult['sections']);
foreach($rawResult['sections'] as $result_sections){
if($result_sections['answers'][1]['mark'] == 'correct') $correct_answer++;
}
}
echo $correct_answer/$RESULTS_count;
对于($i=1;$i获取行)(
$wpdb->prepare(“SELECT*FROM.WPSQT_TABLE_RESULTS.”其中id=$i),数组_A);
$rawResult['sections']=未序列化($rawResult['sections']);
foreach($rawResult['sections']作为$result\u sections){
如果($result_sections['answers'][1]['mark']=='correct')$correct_answer++;
}
}
回显$correct\u answer/$RESULTS\u count;
如果我将代码从FOR循环中取出,并将WHERE id=$I
替换为WHERE id=1
,它就会工作……问题可能是FOREACH不喜欢处于FOR循环中吗
你觉得我该怎么办
编辑:我认为我收到的警告是由于我删除了ID为2的结果,所以当在表行上循环时,其中一行是空的,这就是我收到警告的方式…您应该首先确保数据是数组,或者将其键入数组 Is阵列:
if (is_array($rawResult['sections'])) {
foreach ($rawResult['sections'] as $result_sections) {
if ($result_sections['answers'][1]['mark'] == 'correct')
$correct_answer++;
}
}
排版:
foreach ( (array) $rawResult['sections'] as $result_sections) {
if ($result_sections['answers'][1]['mark'] == 'correct')
$correct_answer++;
}
您应该首先确保数据是数组,或者将其类型转换为数组 Is阵列:
if (is_array($rawResult['sections'])) {
foreach ($rawResult['sections'] as $result_sections) {
if ($result_sections['answers'][1]['mark'] == 'correct')
$correct_answer++;
}
}
排版:
foreach ( (array) $rawResult['sections'] as $result_sections) {
if ($result_sections['answers'][1]['mark'] == 'correct')
$correct_answer++;
}
为什么不修复代码以删除警告?我认为我不能,因为我拥有的数据…您可以发布var_dump的输出($rawResult['sections'));在您取消序列化数据之后?它太长了…但它是一个多维数组。顺便说一句,Jeremy的回答解决了这个问题。为什么不修复代码以删除警告?我不认为我可以,因为我拥有的数据…您可以发布var_dump($rawResult['sections')的输出吗;在取消序列化数据之后?它太长了…但它是多维数组。顺便说一句,Jeremy的回答解决了这个问题。