php - Merging arrays in foreach loop -
i performing csv import database , have trouble adding loop inside array being inserted table.
$lines = 0; $queries = ""; $linearray = array(); $data = array(); if ($csvcontent) $query = "truncate table $databasetable;"; @mysql_query($query); echo "part list ".$databasetable." updated.\n</br>"; foreach(explode($lineseparator,$csvcontent) $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 need have function takes values table , creates array looks $b variable....> $b=array("1","2","3","4","5","6"); foreach($linearray $x){ $b = implode(",", $b); // example $row = $first; $row2 = $first; $row[] = $x."','$b"; // here stays static no me. need cycle... $data[] = implode("','",$row); } } $xx=0; foreach ($data $id) { $xx++; echo $xx; $query="insert csv_test3 values ('$id', '-', '-' , '-', '-')"; $init=mysql_query($query);
in essence need figuring out how merge $b
array foreach loop goes this:
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" }
to this:
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" }
i suppose bunch of iterators:
$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 $item) { print_r(array_merge($item['a'], array($item['b']))); }
the multipleiterator
allows loop on multiple iterators @ same time:
a standard
arrayiterator
feeds$linearray
an
infiniteiterator
continuously loops on array of numbers, limited number of items in$linearray
inside foreach
can "pluck" values both iterators.
Comments
Post a Comment