0

I am performing a csv import to database and have trouble adding a loop inside an array that is being inserted into a table.

$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);

In essence I need help figuring out how to merge the $b array into the foreach loop so that it goes from 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"
}

1 Answer 1

1

I suppose you could do this with a 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 as $item) {
        print_r(array_merge($item['a'], array($item['b'])));
}

The MultipleIterator allows to loop over multiple iterators at the same time:

  1. A standard ArrayIterator that feeds from $linearray

  2. An InfiniteIterator that continuously loops over the array of numbers, limited by the number of items in $linearray

Inside the foreach you can then "pluck" values from both iterators.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.