【问题标题】:looping through multidimensional array from post and save to mysql从post循环遍历多维数组并保存到mysql
【发布时间】:2016-10-05 22:55:57
【问题描述】:

这是我从帖子中得到的数组

Array
(
    [data] => Array
        (
            [Invoice] => Array
                (
                    [itemNo] => Array
                        (
                            [0] => rtgrg
                            [1] => 4t4t
                            [2] => ththt
                            [3] => thth
                        )

                    [itemName] => Array
                        (
                            [0] => rtgrt
                            [1] => 4t5t5
                            [2] => hthtyh
                            [3] => gnghnn
                        )

                    [itemDiscription] => Array
                        (
                            [0] => 5tr5t
                            [1] => t45t4
                            [2] => tyhthtyh
                            [3] => gnghnh
                        )

                    [price] => Array
                        (
                            [0] => 2
                            [1] => 10
                            [2] => 9
                            [3] => 12
                        )

                    [itemQuantity] => Array
                        (
                            [0] => 2
                            [1] => 12
                            [2] => 9
                            [3] => 9
                        )

                    [itemDiscount] => Array
                        (
                            [0] => 11
                            [1] => 0.14
                            [2] => 0.13
                            [3] => 0.1
                        )

                    [itemTotal] => Array
                        (
                            [0] => 333333
                            [1] => 34535
                            [2] => 55555555555555
                            [3] => 666666666
                        )

                    [itemStartDate] => Array
                        (
                            [0] => 
                            [1] => 
                            [2] => 
                            [3] => 2016-06-17
                        )

                    [itemEndDate] => Array
                        (
                            [0] => 
                            [1] => 
                            [2] => 
                            [3] => 
                        )

                    [itemCustomcol] => Array
                        (
                            [0] => 4t4t4
                            [1] => t5trgr
                            [2] => htht
                            [3] => gngh
                        )

                    [itemCustomcol2] => Array
                        (
                            [0] => t4t4t
                            [1] => rtgtr
                            [2] => thth
                            [3] => gng
                        )

                    [itemttax2] => Array
                        (
                            [0] => tax1
                        )

                    [itemttax3] => Array
                        (
                            [0] => tax1
                        )

                    [itemttax4] => Array
                        (
                            [0] => tax1
                        )

                    [itemtCsTax] => Array
                        (
                            [0] => gngn
                        )

                    [itemtCsTaxPer] => Array
                        (
                            [0] => 0.1
                        )

                    [itemtDiscount] => Array
                        (
                            [0] => 0.18
                        )

                    [itemtInvReceived] => Array
                        (
                            [0] => gngn
                        )

                    [itemtInvToReturn] => Array
                        (
                            [0] => gngng
                        )

                    [itemInvDue] => Array
                        (
                            [0] => nghnghng
                        )

                    [itemInvComment] => Array
                        (
                            [0] => hngnhg
                        )

                )

        )

有一个单独的表用于动态输入,如 itemName、itemDiscription 到 itemCustomcol2 以及另一个表用于 itemttax2 到 itemInvComment。

我为多元素数组尝试了 foreach 循环,但这并不适合我,因为它返回一个字符串。其中之一如下所示

    $capture_field_vals ="";
        foreach($_POST["data"]["Invoice"]["itemNo"] as $key => $text_field){
            $capture_field_vals .= $text_field .", ";
        }
echo $capture_field_vals;

我想要的是收集所有元素[0],然后将其插入一行 mysql,然后将另一个元素插入另一行,依此类推。

【问题讨论】:

    标签: php mysql multidimensional-array foreach


    【解决方案1】:

    你有什么理由不能使用下面的格式吗?

    Array
    (
        [data] => Array
            (
                [Invoice] => Array
                    (
                        [0] => Array
                            (
                                [itemNo] => number
                                [itemName] => name
                                [itemDescription] => desc
                                [price] => 0.00
                            ),
                        [1] => Array
                                (
                                    [itemNo] => number
                                    [itemName] => name
                                    [itemDescription] => desc
                                    [price] => 0.00
                                ),...
    

    使用此方法,您可以循环检索发票中每个项目的所有详细信息?

    如果我误解了你的问题,我很抱歉

    【讨论】:

    • Thnx 快速回复并指向这个方向。我对多个元素使用 for 循环,然后我收集它动态变量,然后如果没有错误,那么在另一个循环中我使用插入语句。这是正确的方法吗?我读过确实使用动态变量。
    猜你喜欢
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    相关资源
    最近更新 更多