简体   繁体   English

根据键值将数组插入mysql

[英]insert array into mysql based on key value

I have searched for a simliar solution to my problem, but I am struggling to find it so please excuse me if this has already been asked. 我已经在寻找类似的解决方案来解决我的问题,但是我一直在努力寻找它,因此,如果已经提出了要求,请原谅。 I have my php code as such 我有这样的PHP代码

foreach($data['cells'] as $row) {
    print_r($row);
}

which produces 产生

Array
(
    [1] => ASSET ID
    [2] => SERIAL IN CAPS
    [3] => COMPANYASSETTAG
    [4] => DATE RCVD
    [5] => MFR
    [6] => Type
    [7] => MODEL
    [8] => PRINTER MODEL COMMENTS
    [9] => PART NUMBER
    [10] => R ID
    [11] => GRADE
    [12] => PRICE
    [13] => COLOR CAPABLE (Color or Monochrome)
    [14] => COSMETICALLY ACCEPTABLE (Yes or No)
    [15] => PRINTERCABLEINCLUDED (Yes or No)
    [16] => PRINTER TECHNOLOGY (Laser, Inkjet, 4-1 Laser, 3-1 Laser, 4-1 Ink Jet, 3-1     Ink Jet, Dot Matrix, Plotter, Solid Ink, Thermal)
    [17] => DUPLEX (Yes or No)
    [18] => MULTIFUNCTION (Yes or No)
    [19] => COMMENT  (reason)
    [20] => COSMETICS COMMENT
    [21] => PURCHASE ORDER # (Trailer #)
    [22] => WAYBILL#
)
Array
(
    [1] => CNGYF04230
    [2] => CNGYF04230
    [3] => MISSING
    [4] => 28/12/2012
    [5] => Hewlett Packard
    [6] => Multi-Function Printers
    [8] => 4345X
    [9] => Q3943A
    [11] => G
    [13] => Monochrome
    [14] => Yes
    [15] => No
    [16] => Laser
    [17] => Yes
    [18] => Yes
    [21] => TRDS293
    [22] => HM693800
)

As you can see the key's in the second array are missing ones compared to the above array. 如您所见,与上面的数组相比,第二个数组中的键丢失了。 These correspond to the column. 这些对应于该列。 The reason some are missing in the second is because those fields did not contain any data. 第二个中缺少某些字段的原因是因为这些字段不包含任何数据。 I want to be able to insert these into the appropriate fields in the table in mysql based on the keys. 我希望能够根据键将这些插入到mysql表中的适当字段中。

For instance 例如

foreach($data['cells'] as $row) {
    //print_r($row);
    $sql = "INSERT INTO table (asset_id,serial_in-caps...) VALUES ('$row[0]','$row[1]'...)";
}

As you have numbers as keys in your array, you can loop through your array with a for-loop: 由于数组中有数字作为键,因此可以使用for循环遍历数组:

$keys = array();
foreach($data["cells"] as $row) {
    if(empty($keys)) {
        $keys = $row;
        continue;
    }

    $rowValues = array();
    for($i = 1; $i < count($keys); $i++) {
        if(isset($row[$i]))
            $rowValues[] = $row[$i];
        else
            $rowValues[] = "NULL"; //(or another standard value)
    }
    /* continue with the variable $rowValues */
}

I hope it helps... 希望对您有帮助...

I just found the solution. 我刚刚找到了解决方案。

foreach($data['cells'] as $row) {
    //print_r($row);
    foreach($row as $key => $value) {
        echo "$key is at $value<br>";
    }
}

@atreju @atreju

Your code was perfect, just cleaned up a few missing characters :) 您的代码很完美,只是清理了一些丢失的字符:)

$keys = array();
foreach($data["cells"] as $row) {
    if(empty($keys)) {
        $keys = $row;
        continue;
    }

    $rowValues = array();
    for($i = 1; $i <= count($keys); $i++) {
        if(isset($row[$i]))
            $rowValues[] = $row[$i];
        else
            $rowValues[] = "NULL"; //(or another standard value)
        }
    /* continue with the variable $rowValues */
}

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

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