Jump to content

explod csv file


Go to solution Solved by Jacques1,

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

  • Solution

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...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.