简体   繁体   English

将值从多维数组插入数据库

[英]Insert values from multidimensional array to database

I'm really quite new to PHP and trying to learn it.我对 PHP 真的很陌生,并试图学习它。 Right now I am solving a more difficult task...现在我正在解决一个更困难的任务......

I have form with some repeating fields (not all fields are repeatable).我有一些重复字段的表格(并非所有字段都是可重复的)。 I'm getting this output from whole form:我从整个表格中得到这个 output:

JSON: JSON:

{
    "material": [
        {
            "reference": "11",
            "mnozstvi": "25",
            "level": "1"
        },
        {
            "reference": "56",
            "mnozstvi": "88",
            "level": "2"
        },
        {
            "reference": "44",
            "mnozstvi": "123",
            "level": "1"
        }
    ],
    "pozice": "581",
    "poznamka": "Test",
    "cas": "1619965495",
    "skladnik": "7"
}

Decoded to array:解码为数组:

Array
(
    [material] => Array
        (
            [0] => Array
                (
                    [reference] => 11
                    [mnozstvi] => 25
                    [level] => 1
                )

            [1] => Array
                (
                    [reference] => 56
                    [mnozstvi] => 88
                    [level] => 2
                )

            [2] => Array
                (
                    [reference] => 44
                    [mnozstvi] => 123
                    [level] => 1
                )

        )

    [pozice] => 581
    [poznamka] => Test
    [cas] => 1619965495
    [skladnik] => 7
)

All I want to do is insert these values into one table in MySQL database.我要做的就是将这些值插入 MySQL 数据库中的一个表中。 One row for each material and add other field values (pozice, poznamka, cas, skladnik) to each row.每种材料占一行,并将其他字段值(pozice、poznamka、cas、skladnik)添加到每一行。

So each row will be: reference, mnozstvi, level, pozice, poznamka, cas, skladnik所以每一行将是:reference、mnozstvi、level、pozice、poznamka、cas、skladnik

I know that will be probably easy to you, but I'm totally lost...我知道这对你来说可能很容易,但我完全迷失了......

Many thanks for your answers and help.非常感谢您的回答和帮助。

You can use foreach loop for material key and then use insertion as below:您可以使用 foreach 循环作为材料键,然后使用如下插入:

$collection = array
(
    [material] => Array
        (
            [0] => Array
                (
                    [reference] => 11
                    [mnozstvi] => 25
                    [level] => 1
                )

            [1] => Array
                (
                    [reference] => 56
                    [mnozstvi] => 88
                    [level] => 2
                )

            [2] => Array
                (
                    [reference] => 44
                    [mnozstvi] => 123
                    [level] => 1
                )

        )

    [pozice] => 581
    [poznamka] => Test
    [cas] => 1619965495
    [skladnik] => 7
);

foreach ($collection['material'] as $materialData) {
  //here you can use variables as below.
  echo $materialData['reference'];
  echo $materialData['mnozstvi'];
  echo $materialData['level'];
  echo $collection['pozice'];
  echo $collection['poznamka'];
  echo $collection['cas'];
  echo $collection['skladnik'];
  // you can use variables as above shared syntax and can write your sql insertion code.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM