php - 从post循环遍历多维数组并保存到mysql

标签 php mysql multidimensional-array foreach

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

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,然后将另一个元素插入另一行,依此类推。

最佳答案

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

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
                            ),...

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

如果我误解了你的问题,我深表歉意

关于php - 从post循环遍历多维数组并保存到mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37655637/

相关文章:

php - 如何在 Laravel 中通过翻译从多对多关系中仅获取事件元素

php - 如果进程停止然后执行url,如何编写批处理/CMD文件以每5分钟检查一次?

arrays - 类型安全的矩形多维数组类型

php - 查询表中的行并排除特定行(如果它们存在于另一个表中)

mysql - INSERT INTO...ON DUPLICATE KEY 错误

java - 无法在 Java 控制台中打印 MySql 表的最小值、平均值、最大值

mysql - 简化MySQL查询

python - Numpy:删除 2x2 数组中相邻的重复子数组?

python - 如何仅 reshape numpy中的最后一个维度?

javascript - 必须按 2 次提交按钮才能实际提交 - AJAX