[英]How to check if associative array is empty or null
l want to calculate the total score from a multiple choice and everything is working well but the problem is how can l validate the radio button for multiple choice questions or just check the associative array if it is empty then execute the code below. 我想通过多项选择来计算总分,并且一切都很好,但是问题是如何验证单选按钮是否存在多项选择问题,或者只是检查关联数组是否为空,然后执行以下代码。 l have checked the following links here but failing
我在这里检查了以下链接,但失败了
<?php
if(isset($_POST['btn_sectionA'])){
$id = $_POST['test_id'];
echo "<br>Test ID".$id."<br>";
$sql = "SELECT multiple_choice.mul_question_number, multiple_choice.mul_que_body, multiple_answer.A, multiple_answer.B, multiple_answer.C, multiple_answer.D, multiple_answer.option_Answer\n"
. "FROM multiple_answer\n"
. "JOIN multiple_choice ON multiple_choice.mul_question_number = multiple_answer.question_number\n"
. "WHERE multiple_choice.test_id =".$id." AND multiple_answer.test_id =".$id."";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
echo '<form method="POST" action="../function/sectionA.php">'; //
while($row = mysqli_fetch_assoc($result)) {
echo "Question :".$row["mul_question_number"]." ".$row["mul_que_body"]."<br>";
echo 'A:<input type="radio" name="answer2question['.$row["mul_question_number"].']" value="A"> '.$row["A"].'<br>';
echo 'B:<input type="radio" name="answer2question['.$row["mul_question_number"].']" value="B"> '.$row["B"].'<br>';
echo 'C:<input type="radio" name="answer2question['.$row["mul_question_number"].']" value="C"> '.$row["C"].'<br>';
echo 'D:<input type="radio" name="answer2question['.$row["mul_question_number"].']" value="D"> '.$row["D"].'<br><hr>';
}
}
echo '<input type="hidden" value="'.$id.'" name="test_id"><input type="submit" value="Submit section A" name="btn_sectionA"></form>';
}else{
echo "failed to get questions";
}
?>
The bove code will be posted to this page and some of the code 原始代码将发布到此页面,其中一些代码
foreach($_POST['answer2question'] as $question_number =>$given_answer){
echo "number: ".$question_number." answer:".$given_answer."<br>";
$question_number = mysqli_real_escape_string ($conn, $question_number);
$given_answer = mysqli_real_escape_string ($conn, $given_answer);
$sqlquery = "SELECT question_number FROM multiple_answer WHERE question_number = ".$question_number." AND option_Answer ='".$given_answer."' AND test_id = 1";
$query = mysqli_query($conn,$sqlquery);
if( mysqli_num_rows($query)!== 0 ){
$score += 1;
}
}
You can check an array like the following with is_array
and/or count
: 您可以使用
is_array
和/或count
检查类似以下的数组:
if (is_array($var) === true && count($var) > 0) {
//here your code
}
//or...
if ((count($var) < 1) === false) {
//here your code
}
On the first condition: Don't use only count
because: 第一个条件:不要仅使用
count
因为:
If the parameter is not an array or not an object with implemented Countable interface, 1 will be returned.
如果参数不是数组或不是具有已实现Countable接口的对象,则将返回1。 There is one exception, if array_or_countable is NULL, 0 will be returned.
有一个例外,如果array_or_countable为NULL,则将返回0。 http://php.net/manual/en/function.count.php
http://php.net/manual/zh/function.count.php
Try This way. 尝试这种方式。
$arr = [ 'key' => NULL ];
var_dump(array_key_exists('key', $arr));
var_dump(isset($arr['key']));
Check : 检查:
if(array_key_exists($key, $arra) && is_null($arr[$key]))
{
echo 'key exists with NULL value';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.