如何在php中插入多维数组?

时间:2014-09-10 09:18:20

标签: php mysql arrays multidimensional-array

来自previous question的后续问题。通过从数组中获取值,我的代码就是:

       for ($i=0; $i<count($entries);$i++)
            {
                $pcode = substr($entries[$i][0],0,-2);
                $sku = substr($entries[$i][0],2);
                $aprice = $entries[$i][1];
                $beginv = $entries[$i][2];
                $delivery = $entries[$i][3];
                $endinv = $entries[$i][4];
                $offtake = $entries[$i][5];
                $bo = $entries[$i][6];
                $shelf = $entries[$i][7];
                $gondola = $entries[$i][8];
                $chillers = $entries[$i][9];
                $mass = $entries[$i][10];
                $other = $entries[$i][11];
                $total = $entries[$i][12];

                $statement = "INSERT INTO `report_details` VALUES ('$id', '$pcode', '$sku', '$aprice', '$beginv', '$delivery', '$endinv', '0', '0', '$offtake, '0', '0', '0', '$bo', '$shelf', '$gondola', '$chillers', '$mass', '$other', '$total')";
                $result = mysqlparseradd($statement,$db);

                if($result)
                {echo "YES";}
                else
                {echo "NO";}    
            }

我的阵列是这样的:

Array
 (
    [0] => Array
        (
            [0] => 0111
            [1] => 260.00
            [2] => 23
            [3] => 34
            [4] => 3
            [5] => 54
            [6] => 1
            [7] => 2
            [8] => 4
            [9] => 5
            [10] => 12
            [11] => 23
            [12] => 46
        )

    [1] => Array
        (
            [0] => 0214
            [1] => 22.00
            [2] => 32
            [3] => 4
            [4] => 11
            [5] => 25
            [6] => 4
            [7] => 12
            [8] => 23
            [9] => 5
            [10] => 2
            [11] => 2
            [12] => 44
        )

    [2] => Array
        (
            [0] => 0313
            [1] => 25.00
            [2] => 5
            [3] => 52
            [4] => 12
            [5] => 45
            [6] => 12
            [7] => 5
            [8] => 6
            [9] => 7
            [10] => 12
            [11] => 3
            [12] => 33
        )

)

有了这个,我总是得到NO的值,这意味着它不会被添加到数据库中。我的问题是,我的代码有什么问题,它没有插入到数据库中?当我只插入一行数据但没有多行数据时,我做得很好。

修改

当我打印$statement时,结果是:

INSERT INTO `report_details` VALUES ('JJ1234567890_140822_140824_1020001', '01', '11', '260.00', '23', '34', '3', '0', '0', '54, '0', '0', '0', '1', '2', '4', '5', '12', '23', '46')

只添加了一个数组值。我的mysqlparseradd是一个与mysqli_query类似的功能。它执行查询。

2 个答案:

答案 0 :(得分:2)

您遇到语法错误。

使用此功能。

$statement = "INSERT INTO `report_details` VALUES ('$id', '$pcode', '$sku', '$aprice', '$beginv', '$delivery', '$endinv', '0', '0', '$offtake', '0', '0', '0', '$bo', '$shelf', '$gondola', '$chillers', '$mass', '$other', '$total')";

答案 1 :(得分:1)

当前解决方案的另一种方法,使用数组映射和函数构建一个values子句数组并插入一次。

<?php

$statement = "INSERT INTO `report_details` VALUES ".implode(',', array_map(function($entry) use ($id) {return(format_row($id, $entry));}, $entries));
$result = mysqlparseradd($statement,$db);

if($result)
{
    echo "YES";
}
else
{
    echo "NO";
}   

function format_row($id, $entry)
{
    $pcode = substr($entry[0],0,-2);
    $sku = substr($entry[0],2);
    $aprice = $entry[1];
    $beginv = $entry[2];
    $delivery = $entry[3];
    $endinv = $entry[4];
    $offtake = $entry[5];
    $bo = $entry[6];
    $shelf = $entry[7];
    $gondola = $entry[8];
    $chillers = $entry[9];
    $mass = $entry[10];
    $other = $entry[11];
    $total = $entry[12];
    return "('$id', '$pcode', '$sku', '$aprice', '$beginv', '$delivery', '$endinv', '0', '0', '$offtake', '0', '0', '0', '$bo', '$shelf', '$gondola', '$chillers', '$mass', '$other', '$total')";
}