Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将多维数组插入Mysql表_Mysql_Pdo_Multidimensional Array - Fatal编程技术网

将多维数组插入Mysql表

将多维数组插入Mysql表,mysql,pdo,multidimensional-array,Mysql,Pdo,Multidimensional Array,我有一个多维数组,如下所示: TAB = Array ( [field1] => Array ( [0] => 111 [1] => 777 [2] => 222 ... ) [field2] => Array ( [0] => MAT [1] => MAT [2] => MAT ... ) [field3] => Array ( [0] => 8A [1] => 8B [2] => 8C ..

我有一个多维数组,如下所示:

TAB = Array ( 
    [field1] => Array ( [0] => 111 [1] => 777 [2] => 222 ... ) 
    [field2] => Array ( [0] => MAT [1] => MAT [2] => MAT ... ) 
    [field3] => Array ( [0] => 8A [1] => 8B [2] => 8C ... )
    )
我想用PDO将这个数组的值插入mysql临时表1。 我该怎么办?
谢谢。

假设使用PHP,并假设您有一个表1,其中有两个位置列和一个值列(一列表示第一个数组中的位置,第二列表示第二个数组中的位置,值列表示实际值)。 $dbh是您的数据库处理程序

$keys = array_keys($TAB); // because the array is associative, we need array_keys
for ($i = 0; $i < $TAB.length; $i++){ 
    for ($j = 0; $j < $TAB[keys[$i]].length; $j++){
           $stmt = $dbh->prepare("INSERT INTO TABLE1 (location1, location2, value)  VALUES (:location1, :location2, :value )");
           $stmt->bindParam(':location1', $i);
           $stmt->bindParam(':location2', $j);
           $stmt->bindParam(':value', $TAB[$keys[$i]][$j]);
           $stmt->execute();
        }
}
$keys=array_keys($TAB);//因为数组是关联的,所以我们需要数组_键
对于($i=0;$i<$TAB.length;$i++){
对于($j=0;$j<$TAB[键[$i]])。长度;$j++){
$stmt=$dbh->prepare(“插入表1(位置1,位置2,值)值(:位置1,:位置2,:值)”);
$stmt->bindParam(':location1',$i);
$stmt->bindParam(':location2',$j);
$stmt->bindParam(':value',$TAB[$keys[$i]][$j]);
$stmt->execute();
}
}

您将使用什么语言进行此操作?答案在本主题中: