2013-05-08 85 views
0

我正在執行一個csv導入到數據庫,並且無法在插入到表中的數組內添加一個循環。在foreach循環中合併數組

$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" 
} 

回答

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允許環在多個迭代器同時:

  1. 標準ArrayIterator,通過數量從$linearray

  2. 一種InfiniteIterator連續循環數的陣列之上,limited饋送項目$linearray

在裏面然後你可以從這兩個迭代器中「採集」值。