输入类型文本与数组

Here is what i'm doing so far. I'm printing with AJAX some checkboxes and some text inputs. 1 checkbox and 1 input text for each plauer (from a DB).

Problem is when i submit the data to another php file i only see both for certain cases. Below is the code i'll explain more.

PHP called by AJAX

if($_GET['function']=="chkplayers")
{
     $sql=mysql_query("SELECT * FROM tbl_Player ORDER BY team_name")
     or die(mysql_error());
     $i = 1;
        echo'Players : ';
        echo '<br>';
     while ($row = mysql_fetch_array($sql)) {
        echo '<input type="checkbox" name="chk_player[]" value="'.$row['player_id'].'" /> '.$row['player_name'].' in team '.$row['team_name'];
        echo '<input type="text" name="chk_target[]" placeholder="Target Name"/><br/>';
    $i++;
  }

}

PHP called on submit

if (isset($_POST['chk_player']) && isset($_POST['chk_player'])) {
    $optionArray = $_POST['chk_player'];
    $optionArrayTarget = $_POST['chk_target'];
    for ($i=0; $i<count($optionArray); $i++) {
        echo "i :";
        echo $i;
        echo "Player : ";
        echo $optionArray[$i];
        echo "Target : ";
        echo $optionArrayTarget[$i];
        echo '<br>';

    }
}

If i check every box in a row (not leave empty checkboxes in between) it shows up fine.

Example:

i :0Player : 15Target : A1
i :1Player : 18Target : A2
i :2Player : 14Target : A3

If i leave a box unchecked and then check some more i get this

i :0Player : 15Target : A2
i :1Player : 16Target : 

Anyway way i can fix this?

If i look for $optionArrayTarget i get

i :0Player : 15Target : A1
i :1Player : 14Target :
i :2Player : 12Target : A2
i :3Player : 13Target :
i :4Player : Target : A3
i :5Player : Target : A4
i :6Player : Target :
i :7Player : Target : 

Instead of looping till $optionArray loop till optionArrayTarget

if (isset($_POST['chk_player']) && isset($_POST['chk_target'])) {
   $optionArray = $_POST['chk_player'];
   $optionArrayTarget = $_POST['chk_target'];
   for ($i=0; $i<count($optionArrayTarget); $i++) {
     if (isset($optionArrayTarget[$i]) || trim($optionArrayTarget[$i])!=''){
         echo "i :";
         echo $i;
         echo "Player : ";
         echo $optionArray[$i];
         echo "Target : ";
         echo $optionArrayTarget[$i];
         echo '<br>';
    }
  }
}

so that based on that checked values you can print player and targets, else for every Player present at index 'X' there may be or may not be a Target.

EDITED: One more thing i observed, if (isset($_POST['chk_player']) && isset($_POST['chk_player'])) { the second condition should have been chk_target instead of chk_player

Happy Coding :)

I managed to figure it out. Basically im using array_filter first to remove the empty values then array_values to reorder them and vuala. Thank you for your ime –

if (isset($_POST['chk_player']) && isset($_POST['chk_target'])) {
 $optionArray = $_POST['chk_player'];
 $optionArrayTargetsB4 = array_filter($_POST['chk_target']);
 $optionArrayTargets = array_values($optionArrayTargetsB4);
 for ($i=0; $i<count($optionArray); $i++) {
   .....
  }
}

i used array_filter first to remove the empty values then array_values to reorder them and vuala.