dongxing2263 2017-05-26 09:34
浏览 105
已采纳

在PHP中将多维数组添加到数据库中

Array ( [0] => Array ( [0] => Array ( [name] => Heart Choclates [code] => LFB-P-10 [qty] => 1 [type] => main [price] => 1200 [stock] => 5 [image] => choclates-valentines-day.jpg [quantity] => 12 [expdate] => May 27th 2017 [exptime] => 11:00 PM to 12:00 AM [expdtype] => Mid night delivery ) [1] => Array ( [name] => Birthday Pink [code] => KB-P-5 [qty] => 1 [type] => addon [price] => 600 [stock] => 7 [image] => pink-roses.jpg [expdate] => May 27th 2017 [exptime] => 11:00 PM to 12:00 AM [expdtype] => Mid night delivery ) ) [1] => Array ( [0] => Array ( [name] => Red & Yellow Roses [code] => KB-P-6 [qty] => 1 [type] => main [price] => 800 [stock] => 9 [image] => birthday-red-roses.jpg [expdate] => May 27th 2017 [exptime] => 11:00 PM to 12:00 AM [expdtype] => Mid night delivery ) [1] => Array ( [name] => Signature Cake [code] => KB-P-7 [qty] => 1 [type] => addon [price] => 0 [stock] => 9 [image] => signature-cake.jpg [expdate] => May 27th 2017 [exptime] => 11:00 PM to 12:00 AM [expdtype] => Mid night delivery ) [2] => Array ( [name] => Truffle Cake [code] => KB-P-8 [qty] => 1 [type] => addon [price] => 10 [stock] => 7 [image] => truffle-cake.jpg [expdate] => May 27th 2017 [exptime] => 11:00 PM to 12:00 AM [expdtype] => Mid night delivery ) ) )

I have an array like this.. an array of orders and array of products inside each array of orders. Now i need to add these arrays into database with orders in each row and products seprated by a <br/> . How is it possible? Thanks in advance.

I used this for each to print the code.

<?php
    session_start();
    error_reporting(0);
    print_r($_SESSION["products"]);
    foreach($_SESSION["products"] as $row => $temp){    
?>                                        
<div>
<?php
   foreach($temp as $innerRow => $cart_itm){
    ?>
        <div><?php echo $cart_itm['code']; ?></div>
    <?php
    }
    ?>
</div>
<?php
}
?> 

Expecting this Result

enter image description here

enter image description here

enter image description here

  • 写回答

2条回答 默认 最新

  • dragon4808 2017-05-26 11:20
    关注

    I hope this will get you started:

    $data = array(
        array(
            array(
                'name'  => 'Heart Choclates',
                'code'  => 'LFB-P-10',
                'qty'   => '1' 
            ),
            array(
                'name'  => 'Birthday Pink',
                'code'  => 'KB-P-5',
                'qty'   => '1' 
            )
        ),
        array(
            array(
                'name'  => 'Red & Yellow Roses',
                'code'  => 'KB-P-6',
                'qty'   => '1'
            ),
            array(
                'name'  => 'Signature Cake',
                'code'  => 'KB-P-7',
                'qty'   => '1'
            ),
            array(
                'name'  => 'Truffle Cake',
                'code'  => 'KB-P-8',
                'qty'   => '1'
            )
        )
    );
    
    foreach( $data as $row_item )
    {
        $row = array();
    
        foreach( $row_item as $product )
        {
            foreach( $product as $key => $value )   
            {
                if( isset( $row[ $key ] ) )
                    $row[ $key ] .= $value . ' <br> ';
                else
                    $row[ $key ] = $value . ' <br> ';        
            }
        }
    
        //--- 
        echo 'Row: <br>';
        var_dump( $row );
        echo '<br><br>';
    
        //-- insert the contents of $row array into the database
    }
    

    Output:

    enter image description here

    You can insert the contents of $row array into database.


    EDIT

    Please note that, the above answer is based on your specific question that you asked.

    I still didn't understood what exactly you are doing here. If you are trying to store the details of an order, then this is a poor method of accomplishing it! You should keep separate database table and use a relationship to join together. This is what a relational database is for!

    For example, let the two tables be tblOrderMaster and tblOrderDetails.

    Here, in the tblOrderMaster table, the primary key will be the order_id and you would probably gonna store the total amount, discount amount, total service tax, order date, customer id, order status, etc.

    And in the tblOrderDetails table, you are gonna have several rows, in which we are gonna store each products of a particular order. For example, it will store order id (foreign key), product id, unit price, quantity, tax, total (unit_price x quantity), etc..

    This would be the proper way, I believe.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码