How to store count of all the answers in an array

I am working on a PHP,duing survey,where i have some questions and their options to be choosen.I want to ‘count’ the number of answers in each question,and store them in an array,so that i can compare the count of answers of each question,to find the minimum count .

You’ll want to POST/GET the answers as an array. To do that, you need to name your input fields in this way:


<input name="ans[]" type="number">
<input name="ans[]" type="number">
<input name="ans[]" type="number">
<input name="ans[]" type="number">
<input name="ans[]" type="number">

The “” defines an array in HTML.

Then on the PHP side, to get the number of ‘ans’ checked off, do this:


if(isset($_POST['ans'])){
  $count = count($_POST['ans']);
}

To count all the answers:


if(isset($_POST['ans'])){
  $count = count($_POST['ans']);
  $total = 0;
  foreach($_POST['ans'] as $val){
    $total += $val;
  }
}