Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Looping through the contents of a CSV

Posted on 2009-07-14
6
227 Views
Last Modified: 2012-05-07
I need to loop through the contents of a CSV and pass the values of columns 1 and 6 to a function which then does something with the values, the program should then move to the next row of the csv and grab the same column values and pass those to my function. The program should loop 10 times. Once the loop is complete the first 10 rows of the scv should be deleted.

Can someone help please?

0
Comment
Question by:MayoorPatel
  • 2
  • 2
  • 2
6 Comments
 
LVL 14

Expert Comment

by:flob9
ID: 24849294
this is a repost to this one : http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_24568102.html


<?php
 
//read the data
$lines = split("\n",file_get_contents("file.csv"));
 
 
//parse first 10 lines
$cnt = 0;
foreach($lines as $line)
{
  list($col1,$col2,$col3,$col4,$col5,$col6) = split(";",$line);
  //for each line, call my_func
  my_func($col1,$col2,$col3,$col4,$col5,$col6);
  $cnt++;
  if($cnt==10)
    break;
}
//once done, put the remaining data back into the csv file
if(count($lines)>$cnt)
	file_put_contents("file.csv",join("\n",array_slice($lines,10)));
 
?>

Open in new window

0
 
LVL 9

Expert Comment

by:xBellox
ID: 24849323
Try this (Remeber to change 'test.csv' to your file name, change ";" to your csv delimiter    and your_function to your function name):



<?
	$filename = 'test.csv';
	$f = file($filename);
	
	for ($i=0; ($i<10 && $i<count($f)); $i++) {
		$columns = explode(";", $f[$i]);
		your_function($f[0],$f[1],$f[2],$f[3],$f[4],$f[5]);
	}
	
?>

Open in new window

0
 
LVL 9

Accepted Solution

by:
xBellox earned 500 total points
ID: 24849402
Oops... Little mistake... It isn't $f[0], $f[1]... It ss $columns[0].$columns[1]

<?
		$filename = 'test.csv';
        $f = file($filename);        
        for ($i=0; ($i<10 && $i<count($f)); $i++) {
                $columns = explode(";", $f[$i]);                 
				your_function($columns[0],$columns[1],$columns[2],$columns[3],$columns[4],$columns[5]);
        }      
		if(count($f)>=$i) file_put_contents($filename,join("\n",array_slice($f,10)));
?>

Open in new window

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 1

Author Comment

by:MayoorPatel
ID: 24849826
Flob your solution was NOT complete
0
 
LVL 1

Author Closing Comment

by:MayoorPatel
ID: 31603238
Bravo!
0
 
LVL 14

Expert Comment

by:flob9
ID: 24850940
I don't see the difference
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
php56-php-mcrypt for rhel7 php56 1 42
php help 34 58
Extracting store locations from Google maps or site 2 21
PHP alternative to file_get_contents('php://input') 4 51
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question