我正在对数据库执行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"
}
答案 0 :(得分:1)
我想你可以用一堆迭代器来做到这一点:
$linearray = [['a','z',1], ['a','z',0], ['a','b',1], ['a','c',1]];
$a_iter = new ArrayIterator($linearray);
$b_iter = new LimitIterator(new InfiniteIterator(new ArrayIterator(array("1","2","3","4","5","6"))), 0, count($linearray));
$m_iter = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC);
$m_iter->attachIterator($a_iter, 'a');
$m_iter->attachIterator($b_iter, 'b');
foreach ($m_iter as $item) {
print_r(array_merge($item['a'], array($item['b'])));
}
MultipleIterator
允许同时循环遍历多个迭代器:
从$linearray
ArrayIterator
InfiniteIterator
连续循环遍历数字数组,limited按$linearray
在foreach
内,您可以从两个迭代器中“获取”值。