WordPress.org

Ready to get started?Download WordPress

Forums

foreach Loop Runs Twice - Very Strange (2 posts)

  1. WebTechGlobal
    Member
    Posted 3 years ago #

    This is a strange one. I read a CSV files column titles. I dump them and there are 11. I then loop through them in a foreach, adding each title to an array.

    I do this twice, well it was one loop but I split it during debugging. I dump the result and somehow 11 titles have been entered twice and sometimes 3 times. All within this code...

    // use pear to get file configuration
    	$conf = File_CSV::discoverFormat( $filepath );
    	$fields = File_CSV::read( $filepath, $conf );
    
    echo '<pre>';
    var_dump($fields);
    echo '</pre>';
    
    	// save column titles
    	foreach( $fields as $title )
    	{
    		echo ' TITLE ';
    		$csv['format']['titles'][] = $title;
    	}
    
    	foreach( $fields as $title )
    	{
    		echo ' TITLE ';
    		$csv['format']['titles_sql'][] = eci_cleansqlcolumnname($title);
    	}
    
    	echo '<pre>';
    	var_dump($csv['format']['titles_sql']);
    	echo '</pre>';

    Just to be clear, here is the first dump...

    array(11) {
      [0]=>
      string(2) "ID"
      [1]=>
      string(10) "CHARACTERS"
      [2]=>
      string(5) "TITLE"
      [3]=>
      string(11) "DESCRIPTION"
      [4]=>
      string(5) "PRICE"
      [5]=>
      string(4) "LINK"
      [6]=>
      string(5) "IMAGE"
      [7]=>
      string(4) "DATE"
      [8]=>
      string(4) "CAT1"
      [9]=>
      string(4) "CAT2"
      [10]=>
      string(4) "CAT3"
    }

    now here is the last dump to check what was actually added to my array...

    array(33) {
      [0]=>
      string(2) "id"
      [1]=>
      string(10) "characters"
      [2]=>
      string(5) "title"
      [3]=>
      string(11) "description"
      [4]=>
      string(5) "price"
      [5]=>
      string(4) "link"
      [6]=>
      string(5) "image"
      [7]=>
      string(4) "date"
      [8]=>
      string(4) "cat1"
      [9]=>
      string(4) "cat2"
      [10]=>
      string(4) "cat3"
      [11]=>
      string(2) "id"
      [12]=>
      string(10) "characters"
      [13]=>
      string(5) "title"
      [14]=>
      string(11) "description"
      [15]=>
      string(5) "price"
      [16]=>
      string(4) "link"
      [17]=>
      string(5) "image"
      [18]=>
      string(4) "date"
      [19]=>
      string(4) "cat1"
      [20]=>
      string(4) "cat2"
      [21]=>
      string(4) "cat3"
      [22]=>
      string(2) "id"
      [23]=>
      string(10) "characters"
      [24]=>
      string(5) "title"
      [25]=>
      string(11) "description"
      [26]=>
      string(5) "price"
      [27]=>
      string(4) "link"
      [28]=>
      string(5) "image"
      [29]=>
      string(4) "date"
      [30]=>
      string(4) "cat1"
      [31]=>
      string(4) "cat2"
      [32]=>
      string(4) "cat3"
    }

    You see there is 33 items, where the original array passed by PEAR CSV shows 11 items.

    How does the foreach loop, manage too loop 3 times, sometimes twice. Even with the same file it will be 2 or 3 times. Very random that, nothing is changed.

    I might have a fix that I've not tried and that is to do this, add an ID variable for the array key...

    $id = 0;
    foreach( $fields as $title )
    	{
    		echo ' TITLE ';
    		$csv['format']['titles'][$id] = $title;
    ++$id;
    	}

    I just wanted to get this mentioned somewhere and see if anyone could enlighten me.

    Thanks.

  2. WebTechGlobal
    Member
    Posted 3 years ago #

    Adding an $id variable for the array key seems to have worked!

    Make a lot of Easy CSV Importer users happy. I still don't understand why this happens.

    I suppose the lesson is, where possible, set the array key yourself.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.