2017-05-24 85 views
0

我有一個窗體的每一行有四個值:id,類別名稱,註釋和狀態。但是表單有很多行。當我更新的形式,我需要能夠在數據庫中保存每一行作爲這樣的一行數據:如何保存多維數組

cat_id = 42, cat_name= cedar, notes = notes for cedar, status = active. 

我的問題是,$_POST陣列是不是用這種方式組織:

Array ( 
    [_token] => GJG5lP2bz08OPeEAfn7DKigFWgxkB1ZxdEssVYgO 
    [cat_id] => Array ([42] => 42 [22] => 22 [1] => 1 [31] => 31) 
    [cat_name] => Array ([42] => cedar [22] => Materials [1] => Product 
    [31] => Slates) 
    [notes] => Array ([42] => notes here [22] => Notes here materials [1] 
    => Notes here products [31] => Notes here slates) 
    [status] => Array ([42] => active [22] => active [1] => active [31] 
    => active) 
) 

我需要如何得到這個安排,因此它可以與SQL保存幫助,

使用Laravel,但我預計,在這種情況下,除非無關不知何故,我可以得到它成可用的集合。

謝謝!

回答

0

這是你要找的嗎?

<form action="multiarray.php" method="post"> 
     <input type="text" name="item['token']"> 
     <input type="text" name="item['cat_id'][0]"> 
     <input type="text" name="item['cat_name'][0]"> 
     <input type="text" name="item['notes'][0]"> 
     <input type="text" name="item['status'][0]"> 
     <input type="submit" value="submit"> 
    </form> 

    <?php 

     if(!empty($_POST)){ 

      $array = $_POST;   
      print_r($array); 

     } 

    ?> 

輸出:

陣列([項目] =>數組([ '令牌'] => X)[ 'CAT_ID'] =>數組([0] = X>) ['cat_name'] => Array([0] => x)['notes'] => Array([0] => x)['status'] => Array([0] => x)))

+0

我改變了我的html到你的建議,但是結果數組是相同的。 陣列:2▼ 「_token」=> 「GJG5lP2bz08OPeEAfn7DKigFWgxkB1ZxdEssVYgO」 「項目」=>數組:4 [▼ 「CAT_ID」=>數組:4 [▼ 42 => 「42」 22 =>「22 「 1 => 」1「 31 => 」31「 ] 」 'cat_name「'=>數組:4 [▼ 42 => 」杉樹「 22 => 」材料「 1 =>」 產品「 31 => 」石板「 ] 」 '音符「'=>數組:4 [▶] 」 '狀態'「=>數組:4 [▶] ] ] – Vince