将多维数组插入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();
}
}
您将使用什么语言进行此操作?答案在本主题中: