2013-11-25 35 views
36

我喜歡這個HTML/PHP - 表 - 輸入作爲數組

<form> 
<input type="text" class="form-control" placeholder="Titel" name="levels[level]"> 
<input type="text" class="form-control" placeholder="Titel" name="levels[build_time]"> 

<input type="text" class="form-control" placeholder="Titel" name="levels[level]"> 
<input type="text" class="form-control" placeholder="Titel" name="levels[build_time]"> 

</form> 

一個形式。我想吃點什麼爲$ _ POST輸出類似

Array ( 
    [1] => Array ([level] => 1 [build_time] => 123) 
    [2] => Array ([level] => 2 [build_time] => 456) 
) 

數組我知道我可以做一些像name =「levels [1] [build_time]」等等,但由於這些元素是動態添加的,因此很難添加索引。有沒有其他方法?

編輯:

至於建議,我改變了形式。我現在也包括了我的整個HTML,因爲我覺得我在這裏錯過了一些東西。現在我的HTML:

<div class="form-group"> 
    <label class="col-md-2">Name(z.B. 1)</label> 
    <div class="col-md-10"> 
    <input type="text" class="form-control" placeholder="Titel" name="levels[][level]"> 
    </div> 

    <label class="col-md-2">Bauzeit(In Sekunden)</label> 
    <div class="col-md-10"> 
    <input type="text" class="form-control" placeholder="Titel" name="levels[][build_time]"> 
    </div> 
</div> 

<div class="form-group"> 
    <label class="col-md-2">Name(z.B. 1)</label> 
    <div class="col-md-10"> 
    <input type="text" class="form-control" placeholder="Titel" name="levels[][level]"> 
    </div> 

    <label class="col-md-2">Bauzeit(In Sekunden)</label> 
    <div class="col-md-10"> 
    <input type="text" class="form-control" placeholder="Titel" name="levels[][build_time]"> 
    </div> 
</div> 

輸出我現在得到的是:

[levels] => Array ( 
    [0] => Array ([level] => 1) 
    [1] => Array ([build_time] => 234) 
    [2] => Array ([level] => 2) 
    [3] => Array ([build_time] => 456) 
) 

編輯2:

在你編輯的建議,我編輯的形式和移動的方括號名字的結尾。我現在得到的輸出是:

[levels] => Array ( 
    [level] => Array ( 
    [0] => 1 
    [1] => 2 
) 
    [build_time] => Array ( 
    [0] => 234 
    [1] => 456 
) 
) 

我想這會有點兒工作,但它仍然看起來複雜。沒有更好的方法?

回答

57

只需添加[]那些名字像

<input type="text" class="form-control" placeholder="Titel" name="levels[level][]"> 
<input type="text" class="form-control" placeholder="Titel" name="levels[build_time][]"> 

採取這一模板,然後你甚至可以使用循環添加這些。

然後,您可以根據需要動態添加這些內容,而無需提供索引。 PHP將會像你預期的場景例子一樣提取它們。

編輯

對不起我錯了地方,這將使每一個新的值作爲新的數組括號。現在使用更新後的代碼,這會給你下面的數組結構

levels > level (Array) 
levels > build_time (Array) 

兩個子陣列上的相同索引會給你一對。例如

echo $levels["level"][5]; 
echo $levels["build_time"][5]; 
+0

請看我的編輯,我做錯了什麼? –

+0

抱歉抱歉,我的方括號在錯誤的地方。請將它們移動到名稱末尾,例如我的新編輯。 –

+0

我看到你再次編輯你的文章:)我現在得到了相同的結構,但它仍然不是我最初想要的。這是做到這一點的最佳方式? –

4

除了: 爲那些誰擁有一個空的POST變量,不使用此:

name="[levels][level][]" 

,而使用這個(因爲它已經在這裏在這個例子中):

name="levels[level][]" 
15

如果是你合適索引數組,你可以這樣做:

<form> 
    <input type="text" class="form-control" placeholder="Titel" name="levels[0][level]"> 
    <input type="text" class="form-control" placeholder="Titel" name="levels[0][build_time]"> 

    <input type="text" class="form-control" placeholder="Titel" name="levels[1][level]"> 
    <input type="text" class="form-control" placeholder="Titel" name="levels[1][build_time]"> 

    <input type="text" class="form-control" placeholder="Titel" name="levels[2][level]"> 
    <input type="text" class="form-control" placeholder="Titel" name="levels[2][build_time]"> 
</form> 

...以實現:

[levels] => Array ( 
    [0] => Array ( 
    [level] => 1 
    [build_time] => 2 
) 
    [1] => Array ( 
    [level] => 234 
    [build_time] => 456 
) 
    [2] => Array ( 
    [level] => 111 
    [build_time] => 222 
) 
) 

但是,如果你刪除1對從形式的中間投入(動態,我認爲),那麼你會得到孔陣列中,除非你更新輸入名稱...

5

HTML:使用名稱爲

<input name="levels[level][]"> 
<input name="levels[build_time][]"> 

PHP:

$array = filter_input_array(INPUT_POST); 
$newArray = array(); 
foreach (array_keys($array) as $fieldKey) { 
    foreach ($array[$fieldKey] as $key=>$value) { 
     $newArray[$key][$fieldKey] = $value; 
    } 
} 

$ newArray將保存數據,你想要

Array ( 
    [0] => Array ([level] => 1 [build_time] => 123) 
    [1] => Array ([level] => 2 [build_time] => 456) 
) 
+2

我不知道爲什麼這是downvoted,但這正是我所需要的。 – Dediqated