从$ _POST中提取数组数据

时间:2012-11-05 05:26:59

标签: php arrays loops foreach merge

我有两个MYSQL表'Piece'和'Order1'

PHP中的

我想循环遍历这个$ _POST数组中的每个Order(及其中的部分)并将其存储在我的数据库中。

我试图做一个while循环,存储每个订单但却无法获得每个订单下面的碎片。我想也许这不是可行的方法。

如果有人给我一个例子或建议那就太棒了。

    [order] => Array
            (
                [details] => Array
                    (
                        [0] => Array
                            (
                                [order1_itemcode] => 
                                [order1_description] => 
                                [order1_itemprice] => 
                                [order1_packagetype] => 0
                                [pieces] => Array
                                    (
                                        [0] => Array
                                            (
                                                [piece_length] => 235
                                                [piece_width] => 235
                                                [piece_height] => 235
                                                [piece_unittype] => cm
                                                [piece_actualweight] => 
                                                [piece_weighttype] => kg
                                            )

                                        [1] => Array
                                            (
                                                [piece_length] => 346
                                                [piece_width] => 346
                                                [piece_height] => 346
                                                [piece_unittype] => cm
                                                [piece_actualweight] => 
                                                [piece_weighttype] => kg
                                            )

                                    )

                            )

                        [1] => Array
                            (
                                [order1_itemcode] => 
                                [order1_description] => 
                                [order1_itemprice] => 
                                [order1_packagetype] => 0
                                [pieces] => Array
                                    (
                                        [0] => Array
                                            (
                                                [piece_length] => 46
                                                [piece_width] => 346
                                                [piece_height] => 346
                                                [piece_unittype] => cm
                                                [piece_actualweight] => 
                                                [piece_weighttype] => kg
                                            )

                                        [1] => Array
                                            (
                                                [piece_length] => 346
                                                [piece_width] => 223
                                                [piece_height] => 235
                                                [piece_unittype] => cm
                                                [piece_actualweight] => 
                                                [piece_weighttype] => kg
                                            )

                                    )

                            )

                    )

            )

4 个答案:

答案 0 :(得分:2)

试试这个:

   foreach ($_POST['order']['details'] as $detail)
   {
       foreach($detail['pieces'] as $pieces)
       {
          //store($pieces['piece_length']);
          //store($pieces['piece_width']);
          //store($pieces['piece_height']);
          //store($pieces['piece_unittype']);
          //store($pieces['piece_actualweight']);
          //store($pieces['piece_weighttype']);
       }
    }

答案 1 :(得分:0)

答案 2 :(得分:0)

最好使用

 foreach($_POST['order']['details'] as $key=>$order)
 {
    if(is_array($order['pieces']) && $key=='pieces')
    {
      foreach($order['pieces']) as $pices)
      $insert[]=implode(',',$pices);//array for later insert,you should do necessary validation and should deal with mysql injection
    }

 }

答案 3 :(得分:0)

试试这个

foreach($orders as $order)
{
   //store($order)

   $pieces = $order['pieces'];
   foreach($pieces as $a_pieces)
   {
      //store($a_pieces);
   }
}