我正在对数据库执行 csv 导入,并且在插入到表中的数组中添加循环时遇到问题。
$lines = 0;
$queries = "";
$linearray = array();
$data = array();
if ($csvcontent) $query = "TRUNCATE TABLE $databasetable;";
@mysql_query($query);
echo "Part list for ".$databasetable." updated.\n</br>";
foreach(explode($lineseparator,$csvcontent) as $value){
$lines++;
$value = trim($value," \t");
$value = str_replace("\r","",$value);
$value = str_replace("'","\'",$value);
$linearray = explode($fieldseparator,$value);
$linemysql = implode("','",$linearray);
$first = array_splice($linearray, 0, 2);
<... Here I need to have a function that takes certain values from a table and creates an array that basically looks like $b variable....>
$b=array("1","2","3","4","5","6");
foreach($linearray as $x){
$b = implode(",", $b); // example
$row = $first;
$row2 = $first;
$row[] = $x."','$b"; // here it just stays as static which is no good to me. I need it to cycle...
$data[] = implode("','",$row);
}
}
$xx=0;
foreach ($data as $id) {
$xx++;
echo $xx;
$query="INSERT INTO csv_test3 VALUES ('$id', '-', '-' , '-', '-')";
$init=mysql_query($query);
本质上,我需要帮助弄清楚如何将$b
数组合并到 foreach 循环中,以便它从此开始:
array(18) {
[0]=>
string(23) "a','z','1','1,2,3,4,5,6"
[1]=>
string(23) "a','z','0','1,2,3,4,5,6"
[2]=>
string(23) "a','z','1','1,2,3,4,5,6"
[3]=>
string(23) "a','z','1','1,2,3,4,5,6"
[4]=>
string(23) "a','z','0','1,2,3,4,5,6"
[5]=>
string(23) "a','z','0','1,2,3,4,5,6"
[6]=>
string(23) "b','y','1','1,2,3,4,5,6"
[7]=>
string(23) "b','y','0','1,2,3,4,5,6"
[8]=>
string(23) "b','y','0','1,2,3,4,5,6"
[9]=>
string(23) "b','y','1','1,2,3,4,5,6"
[10]=>
string(23) "b','y','0','1,2,3,4,5,6"
[11]=>
string(23) "b','y','0','1,2,3,4,5,6"
[12]=>
string(23) "c','x','1','1,2,3,4,5,6"
[13]=>
string(23) "c','x','1','1,2,3,4,5,6"
[14]=>
string(23) "c','x','1','1,2,3,4,5,6"
[15]=>
string(23) "c','x','1','1,2,3,4,5,6"
[16]=>
string(23) "c','x','0','1,2,3,4,5,6"
[17]=>
string(23) "c','x','0','1,2,3,4,5,6"
}
对此:
array(18) {
[0]=>
string(23) "a','z','1','1"
[1]=>
string(23) "a','z','0','2"
[2]=>
string(23) "a','z','1','3"
[3]=>
string(23) "a','z','1','4"
[4]=>
string(23) "a','z','0','5"
[5]=>
string(23) "a','z','0','6"
[6]=>
string(23) "b','y','1','1"
[7]=>
string(23) "b','y','0','2"
[8]=>
string(23) "b','y','0','3"
[9]=>
string(23) "b','y','1','4"
[10]=>
string(23) "b','y','0','5"
[11]=>
string(23) "b','y','0','6"
[12]=>
string(23) "c','x','1','1"
[13]=>
string(23) "c','x','1','2"
[14]=>
string(23) "c','x','1','3"
[15]=>
string(23) "c','x','1','4"
[16]=>
string(23) "c','x','0','5"
[17]=>
string(23) "c','x','0','6"
}