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 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)