我不想捕获下一个数组值并将其与当前数组值进行比较。我尝试在$key+1
中使用foreach
执行此操作,但除了获取前一个值之外,它会运行当前的两次。有人能帮助我吗?
var_dump
数组
array(5) { [0]=> string(1) "1" [1]=> string(1) "1" [2]=> string(1) "1" [3]=> string(1) "0" [4]=> string(0) "" }
代码:
foreach ($q_switch as $key => $value) {
if ( $q_switch[$key+1] == 0 || empty($q_switch[$key+1]) ) {
$question_html .= ""
} else {
$question_html .= '<div class="question-wrapper">
<form method="post">
<div class="wp-postquiz-question">' . $q_question . '</div>
<div class="wp-postquiz-question-options">
<select name="answer-' . $question_no . '">
<option value="A">' . $q_option_1 . '</option>
<option value="B">' . $q_option_2 . '</option>
<option value="C">' . $q_option_3 . '</option>
<option value="D">' . $q_option_4 . '</option>
</select>
</div>
<input type="hidden" name="question-no" value="' . $question_no . '">
<input type="hidden" name="post-id" value="' . $postid . '">
<input type="hidden" name="action" value="answer-submitted">
<input type="submit" name="submit" class="button primary" value="Answer!">
</form>
</div>';
}
}
答案 0 :(得分:1)
我不确定您要实现的目标但是通过查看您的代码,您似乎想要逃避由 0 和'组成的值(null / empty)值。因此,不要使用foreach
循环,而是更好地使用array_filter
函数,只需将您的其余代码发布在foreach
$q_switch = array('0'=>"1",'1'=>"1",'2'=>"1",'3'=>"0",'4'=>"");
$result = array_filter($q_switch);
print_r($result);//Array ( [0] => 1 [1] => 1 [2] => 1 )
答案 1 :(得分:1)
感谢所有解决方案的人。我尝试的代码工作正常,但问题是我在int
中取值,结果是值存储在字符串中。此外,我最终删除了foreach
循环,并通过在当前问题中添加1
来捕获下一个值。所以基本上整个功能看起来像下面,以防有人需要它......
/**
* Get Question from Database for the post
* @param [int] $question_no
* @return [string]
*/
function wp_postquiz_display_question($question_no) {
// Get the total answered questions by the user.
do_action('wp_postquiz_total_answered_questions_by_user' );
// Check if Post Quiz is enabled or disabled.
global $questions, $answeredQuestions;
// Create $q_switch array
$q_switch = array();
// Get the post id in which quis is being used.
$postid = get_the_ID();
// Question Details
$q_post_content = $questions['question-' . $question_no . '']['content'];
$q_question = $questions['question-' . $question_no . '']['question'];
$q_option_1 = $questions['question-' . $question_no . '']['options']['option-1'];
$q_option_2 = $questions['question-' . $question_no . '']['options']['option-2'];
$q_option_3 = $questions['question-' . $question_no . '']['options']['option-3'];
$q_option_4 = $questions['question-' . $question_no . '']['options']['option-4'];
// HTML to return
$question_html = '<div class="wp-postquiz-wrapper">
<div class="wp-postquiz content">' . $q_post_content . '</div>';
$currnetQuestion = "post-" . $postid . "-question-" . $question_no;
// If previous question is answered don't include the question for it.
if ( !in_array($currnetQuestion, $answeredQuestions) ){
$next = $question_no + 1;
$q_switch = $questions['question-' . $question_no . '']['q_switch'];
$q_switch_next = $questions['question-' . $next . '']['q_switch'];
if ( $q_switch == 1 && !empty($q_switch_next) ) {
$question_html .= '<div class="question-wrapper">
<form method="post">
<div class="wp-postquiz-question">' . $q_question . '</div>
<div class="wp-postquiz-question-options">
<select name="answer-' . $question_no . '">
<option value="" disabled selected>Select the correct answer</option>
<option value="A">' . $q_option_1 . '</option>
<option value="B">' . $q_option_2 . '</option>
<option value="C">' . $q_option_3 . '</option>
<option value="D">' . $q_option_4 . '</option>
</select>
</div>
<input type="hidden" name="question-no" value="' . $question_no . '">
<input type="hidden" name="post-id" value="' . $postid . '">
<input type="hidden" name="action" value="answer-submitted">
<input type="submit" name="submit" class="button primary" value="Answer!">
</form>
</div>';
}
}
$question_html .= '</div>';
echo $question_html;
}