Jump to content

explod csv file


kimjessen

Recommended Posts

Hello yes so I let myself be carried away, and then came a new small challenge.
In the graph code is the  csv file opened and soteret with a "explod" function.
and it work fine as long as I only have one column with y data and one with x data.
but when I start to get more column's it does not work.
 
I've created a short example below.
<?php
	

	define("LOG_FILE", "./data/test111/test111-2015-01-17.csv");
	
	$times = array();
	$values = array();	
	$values_test1 = array();
	
	
	$file_lines = file(LOG_FILE , FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
	
	foreach($file_lines as $line_num => $line_value) {
		$line_elements = explode(",", $line_value);
		$times[] = date($line_elements[0]);
		$values[] = $line_elements[1];
		$values_test1[] = $line_elements[2];
		
	}


	echo $values[2];
	
	
?>
the control echo I make "echo $values[2];" works fine, there is be the expected value.
but if I try to "echo $values_test1[2];" so there is nothing.
the CSV file looks like this.
Date,temp1,temp2
2015-01-23-12-00,54,59
2015-01-23-12-01,54,59
2015-01-23-12-02,54,59
2015-01-23-12-03,54,58
2015-01-23-12-04,54,58
2015-01-23-12-05,54,58
2015-01-23-12-06,54,58
2015-01-23-12-07,54,58
2015-01-23-12-08,54,58
2015-01-23-12-09,54,58
2015-01-23-12-10,54,58
2015-01-23-12-11,53,58
2015-01-23-12-12,53,58
2015-01-23-12-13,53,58
2015-01-23-12-14,53,58
 
why can I not get the temp2 values into $ values_test1 []
 
thank you very much

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/294200-explod-csv-file/
Share on other sites

The code works just fine for me (using the test data you've provided). So what exactly is the problem? Have you inspected the variables with var_dump? What's the content of $file_lines and $line_elements?

 

By the way, PHP can parse CSV files, so no need to mess with file() and explode().

Link to comment
https://forums.phpfreaks.com/topic/294200-explod-csv-file/#findComment-1504083
Share on other sites

  • 3 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.