Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.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
如何在php fo数组中插入字符串值?_Php_Mysql_Arrays_String - Fatal编程技术网

如何在php fo数组中插入字符串值?

如何在php fo数组中插入字符串值?,php,mysql,arrays,string,Php,Mysql,Arrays,String,我有一个数组 Array( [0] => Column 1, Column 2 ,Column 3,Column 4,Column 5,Column 6 [1] => 19506,7254,321878,8,1,60 [2] => 187486,685,377,3,0,0 [3] => 187498,682285,3354,45,1,400 [4] => 184

我有一个数组

   Array(  
         [0] => Column 1, Column 2 ,Column 3,Column 4,Column 5,Column 6
         [1] => 19506,7254,321878,8,1,60 
         [2] => 187486,685,377,3,0,0 
         [3] => 187498,682285,3354,45,1,400 
         [4] => 18498,681285,38959,2,0,0 
      )
使用循环或任何其他解决方案存储到mysql表中 如何在数据库中插入
我在一个表中有完全相同的列

使用下面的代码段将其转换为数组以保存到数据库中

// replace spaces with "" and explode with comma
$keys = explode(",", str_replace(" ","",$arr[0]));
array_shift($arr); // remove first index
$arr = array_map(function($a) use($keys){
    // first value as keys and rest array values exploding and combining as key value pair
    return array_combine($keys, explode(",",$a));
}, $arr);

创建插入查询的代码

$sql = "INSERT INTO table_name (".implode(",",$keys).") VALUES ";
foreach($arr as $data){
    $sql .= "(".implode(",",$data)."),";
}
$sql = rtrim($sql,",");
echo $sql;
输出

INSERT INTO table_name (Column1,Column2,Column3,Column4,Column5,Column6)  
VALUES (19506,7254,321878,8,1,60),(187486,685,377,3,0,0), 
(187498,682285,3354,45,1,400),(18498,681285,38959,2,0,0)

请您添加更多关于您的连接方法的信息,以及到目前为止您尝试了什么。这个阵列是如何产生的?处理数据似乎是一种非常奇怪的方式。