如何保存多维数组

时间:2017-05-24 20:24:35

标签: php arrays

我有一个表单,其中表单的每一行都有四个值:id,类别名称,注释和状态。但是表单有很多行。当我更新表单时,我需要能够将DB中的每一行保存为一行数据,如下所示:

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,但我希望在这种情况下无关紧要,除非以某种方式我可以将它变成可用的集合。

谢谢!

1 个答案:

答案 0 :(得分: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);

        }

    ?>

输出:

  

数组([item] =&gt;数组(['token'] =&gt; x)['cat_id']   =&GT;数组([0] =&gt; x)['cat_name'] =&gt;数组([0] =&gt; x)['notes'] =&gt;数组([0] =&gt; x)['status'] =&gt;数组([0] =&gt; x)))