Submitting multi-dimensional array to PHP

Viewed 40

I want to submit a table which contain many rows to database.

So for I'm naming the field with empty key with the hope that php will populate them itself.

Moreover I do have a jQuery code that can add a new line to the table.

So basically this is how looks like a row in my form:

<tr class="copytr">
                    <td nowrap>#</td>
                    <td><select required name="chantier[][spb_etapes_chantier_room]">
                            {foreach $list_rooms as $room}
                                <option value="{$room.spb_gestion_room__id}"><i class="{$room.spb_gestion_room__icon}"></i> {$room.spb_gestion_room__name}</option>
                            {/foreach}
                        </select>
                    </td>
                    <td><select required name="chantier[][spb_etapes_chantier_etat]">
                            <option value="0" >En attente</option>
                            <option value="1" >Démarré</option>
                            <option value="2" >En Pause</option>
                            <option value="3" >Terminé</option>
                        </select>
                    </td>
                    <td>
                        <input required type="text" name="chantier[][spb_etapes_chantier_avancement]" >
                    </td>
                    <td>
                        <input  required type="text" name="chantier[][spb_etapes_chantier_nom_etape]" >
                    </td>
                    <td>
                        <button type="button" class="btn u-btn-red btn-sm delete_row" onclick="return confirm('Voulez-vous vraiment supprimer cette ligne ?')">
                            <i class="fa fa-trash-o"></i>
                        </button>
                        </form>
                    </td>
                </tr>

The trouble I have is that I can not access to any of this line, because all fields are separated.

My

echo '<pre>';
print_r($_POST['chantier']);
var_dump($_POST['chantier']);
echo '</pre>';

returns me:

Array
(
    [0] => Array
        (
            [spb_etapes_chantier_room] => 1
        )

    [1] => Array
        (
            [spb_etapes_chantier_etat] => 0
        )

    [2] => Array
        (
            [spb_etapes_chantier_avancement] => 0
        )

    [3] => Array
        (
            [spb_etapes_chantier_nom_etape] => démo
        )

)
array(4) {
  [0]=>
  array(1) {
    ["spb_etapes_chantier_room"]=>
    string(1) "1"
  }
  [1]=>
  array(1) {
    ["spb_etapes_chantier_etat"]=>
    string(1) "0"
  }
  [2]=>
  array(1) {
    ["spb_etapes_chantier_avancement"]=>
    string(1) "0"
  }
  [3]=>
  array(1) {
    ["spb_etapes_chantier_nom_etape"]=>
    string(5) "démo"
  }
}

I was expecting something like chantier[0][spb_etapes_chantier_room] =>1 etc. and not many array as number of fields.

I've been searching for many documentations but it speak only on associative array or enumerated array and there is not much about multi-dimensional arrays.

0 Answers
Related