为数组和值插入带数组的查询

时间:2017-04-05 02:10:31

标签: c# sql

我想插入一组灵活输入的数据。

例如,

<?php

$fields = [
    [
        "state" => "nsw",
        "address" => "8 Test Street",
        "phone" => "0000 000 000",
        "email" => "info@123.com.au",
    ],
    [
        "state" => "wa",
        "address" => "5 Test Street",
        "phone" => "0000 000 000",
        "email" => "info@123.com.au",
    ],
    [
        "state" => "qld",
        "address" => "3 Test Street",
        "phone" => "0000 000 000",
        "email" => "info@123.com.au",
    ],
    [
        "state" => "nsw",
        "address" => "1 Test Street",
        "phone" => "0000 000 000",
        "email" => "info@123.com.au",
    ]
];

$new = [];
foreach ($fields as $field) {
    $state = $field['state'];
    unset($field['state']);
    $new[$state][] = $field;
}

print_r($new);

Array
(
    [nsw] => Array
        (
            [0] => Array
                (
                    [address] => 8 Test Street
                    [phone] => 0000 000 000
                    [email] => info@123.com.au
                )

            [1] => Array
                (
                    [address] => 1 Test Street
                    [phone] => 0000 000 000
                    [email] => info@123.com.au
                )

        )

    [wa] => Array
        (
            [0] => Array
                (
                    [address] => 5 Test Street
                    [phone] => 0000 000 000
                    [email] => info@123.com.au
                )

        )

    [qld] => Array
        (
            [0] => Array
                (
                    [address] => 3 Test Street
                    [phone] => 0000 000 000
                    [email] => info@123.com.au
                )

        )

)

我怎么想让这段代码成功运作?

  

解决方案:

感谢所有试图回答我的人,但我实际上是通过使用String.Join来解决它的 我只想创建一个包含添加,编辑,删除查询的类文件,以便在我创建的每个表单中使用。当我想使用查询类文件时,我可以更灵活地插入我的数据。

1 个答案:

答案 0 :(得分:-1)

如果我理解你的问题,你可以在这里找到解决方案 Insert XML formed Data into Table (SQL)