Q:

How to insert data to multiple column database from indexed array text input name PHP using one foreach?

first I hope You understand the question I made because I'm not English. I'm making simple football league table. So I want to record all the team's goal between home team and away team including the players assists, and own goals using PHP form. the problem is I want to SUBMIT them in ONE foreach() instead of foreach() for inserting the goals, foreach() for inserting the assists, and foreach() for inserting the own goals.
I have tried using foreach(), and I just only know that is for ONE indexed array variable or Associative Array;
The code of form input goals, asssists, and own goal (this is from JSON encode):
'.... <td><input class="goal-home form-control col-6" name="goal_home['+data.player[i].id_player+']" type="number" value="0"></td>'+
'<td><input class="assist-home form-control col-6" name="assist_home['+data.player[i].id_player+']" type="number" value="0"></td>'+
'<td><input class="owngoal-home form-control col-6" name="owngoal_home['+data.player[i].id_player+']" type="number" value="0"></td>'+ ....

The code for get the data from home team side:
$goal_home = $this->input->post('goal_home');
$assist_home = $this->input->post('assist_home');
$owngoal_home = $this->input->post('owngoal_home');

And i have tried to insert the data to database something like this:
 //The code for inserting each player's goal
    foreach ($goal_home as $goal => $val) {
                        $this->m->query("UPDATE tbl_player 
                                        SET goal = (goal + $val)
                                        WHERE id_player = $goal
                                        ");
                    }
    //The code for inserting each player's assist
    foreach ($assist_home as $assist => $val) {
                        $this->m->query("UPDATE tbl_player 
                                        SET assist = (assist + $val)
                                        WHERE id_player = $assist
                                        ");
                    }
    //The code for inserting each player's own goals
    foreach ($owngoal_home as $owngoal => $val) {
                        $this->m->query("UPDATE tbl_player 
                                        SET owngoal = (owngoal + $val)
                                        WHERE id_player = $owngoal
                                        ");
                    }

but my expecting something like this (I know this is wrong):
    foreach (($goal_home as $goal => $val1), ($assist_home as $assist => $val2), ($owngoal_home as $owngoal => $val3)) {

$this->m->query("UPDATE tbl_player 
                      SET goal = (goal + $val1), assist = (assist + $val2) ,owngoal = (owngoal + $val3)
                      WHERE id_player = $goal
                     ");

I know that was wrong but I don't know the best way to explain this, I've been searching this similiar problem, but no one can resolve or maybe I don't understand. Thank you

A:

In all of your arrays, you use the same indexes. So an easy way would be to run trough one of the arrays and use the index to get the data from all arrays with the given index within each loop. 
$goal_home    = $this->input->post('goal_home');
$assist_home  = $this->input->post('assist_home');
$owngoal_home = $this->input->post('owngoal_home');

foreach ($goal_home as $id => $dummy) {
 $this->m->query("UPDATE
                        tbl_player 
                     SET
                        Goal    = (goal    + $goal_home[$id]),
                        assist  = (assist  + $assist_home[$id])
                        owngoal = (owngoal + $owngoal_home[$id])
                   WHERE
                        id_player = $id");
}

But you could also cange the names of your input fields from
<input name="goal_home['+data.player[i].id_player+']">
<input name="assist_home['+data.player[i].id_player+']">
<input name="owngoal_home['+data.player[i].id_player+']">

to
<input name="player['+data.player[i].id_player+'][goals]">
<input name="player['+data.player[i].id_player+'][assists]">
<input name="player['+data.player[i].id_player+'][owngoals]">

Then you get one Array with all the data in it and you can go trough the data like this:
$data = $this->input->post('player');

foreach ($data as id => $item) {
 $this->m->query("UPDATE
                        tbl_player 
                     SET
                        Goal    = (goal    + $item['goals']),
                        assist  = (assist  + $item['assists'])
                        owngoal = (owngoal + $item['owngoals'])
                   WHERE
                        id_player = $id");
}

