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条)

报告相同问题?

悬赏问题

  • ¥15 qt 散点图自定义图形
  • ¥100 将USDZ文件转化为带颜色的OBJ文件
  • ¥15 对象代号: , 表单: 不存在!
  • ¥15 WebSocket的问题
  • ¥15 centos上启动kylin后网页报错404
  • ¥20 使用hackrf进行信号收发时接收到的信号幅度太小
  • ¥15 WebSocket的问题
  • ¥15 BDSBAS-B1C和B1C信号有什么不同
  • ¥15 在半圆平面内随机生成点坐标
  • ¥15 系统容量变化的几种多址方式TDMA, CDMA,FDMA,OFDMA 对比,应该给的是一个曲线 图,随着系统容量的增加,几种多址方式性能的对比 图,MATLAB程序仿真折线图