Tried finding a solution on SO and I found multiple questions that are similar but none of them were limited to a number of keys.
I am building a questionaire that has an algorithm based on which it recommends you three winners out of 7 based on the points that they got on each question answer. The table has 10 columns: id, question_no, answer and 7 that are holding the point values.
Here is the code:
<?php
include_once "connect.php";
$question = array();
$question[1] = mysqli_real_escape_string($con, $_POST['question_01']);
// the rest of the questions go here
$question[10] = mysqli_real_escape_string($con, $_POST['question_10']);
$i = 0;
$array_sum=[];
while ($i < 10){
$i++;
$sql = "SELECT * FROM partners WHERE question_no = $i AND answer = '".$question[$i]."'";
$result = mysqli_query($con, $sql);
$final_array_1 = array();
while ($row = mysqli_fetch_array($result, MYSQLI_NUM))
{
$final_array_1 = $row;
$array_sum = array_map(function () {
return array_sum(func_get_args());
}, $array_sum, $final_array_1);
print_r($final_array_1);
echo "<br/>";
}
}
The output of this would be something like:
Array ( [0] => 1 [1] => 1 [2] => A [3] => 5 [4] => 5 [5] => 5 [6] => 5 [7] => 5 [8] => 5 [9] => 5 )
Array ( [0] => 6 [1] => 2 [2] => B [3] => 0 [4] => 0 [5] => 0 [6] => 8 [7] => 8 [8] => 0 [9] => 0 )
And so on for the other 8 arrays.
Basically now I need to add up the values of all the keys from 3 to 9 but since I got these values in a "while" I'm not really sure how to achieve this.
I also need to keep those individual arrays that I get so I can store the options that the user selected in the form in the database.
Let me know if you need any additional details.