0
<?php

$file_handle = fopen("info.csv", "r");
while (!feof($file_handle) ) {
$data = fgetcsv($file_handle, 1024);
echo"$data[0] . $data[1]";
}

fclose($file_handle);

?>

I want to get the specific cell value (2,2) from csv in php, how can i do that ? Thank you very much!

Edited by cliffcc: n/a

3
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by cliffcc
0

you need to count the cells and you need to know what you are expecting.

$linecount = 0;
$cellcnt = 0;
while(!feof($handle)) {
	$data = fgets($handle);
	$listing = explode(",", $data);	 // break out each cell on the comma delimeter
	// iterate over each cell					
	$linecount++;			
	// $listing is an entire line.  we now read each cell...					
	foreach($listing as $key => $value) {	
	// first iteration linecount = 1 so just skip it.
	// you don't have to put this conditional on but if you truly only want the value of cell 2,2
	// the value you are looking for is when $key = 1, because it is 0 based array 0 is cell 1, 1 is cell 2 so case 1 should return what you are looking for.
		if ($linecount == 2) {
			switch($key) {
				case 0:
				  echo "cell 2,1 value = " . $value ."<br>";
				  break;					
				case 1:
				  echo "cell 2,2 value = " . $value ."<br>";
				  break;
				case 2:
				  echo "Cell 2,3 value = " . $value ."<br>";
				  break;
				case 3:
				  echo "Cell 2,4 value = " . $value ."<br>";
				  break;
				default:
				  echo "more values = " . $value;						
			}
		// increase you cell count					
		}
		$cellcnt++;
	}					
}
0

I used the following technique:

explode on linebreak (to get rows)

loop through the ensuing array and explode that on ',' to get values

so first cell will be $cell[1][0] as the $cell[0][] values will be the field headers
in general, to get any cell content, do $cell[$row][$column-1].

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.