jadeg Posted April 3, 2014 Share Posted April 3, 2014 I have a csv file , how do I access the value in a particular row and column. The file has 11 coloums and 3599 row. How do I for instance access the value in column 5 row 100. In the end what I am looking to do is have a function that multiplies every value in the csv my certain percentage very minute. My code so far looks like this $ftp_server="eeeproject.com"; $ftp_user_name="user"; $ftp_user_pass="password"; $conn_id = ftp_connect($ftp_server) or die ("Couldn't connect to $ftp_server"); $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); if($login_result) { echo '<h1> Last Second Reading for Bridgetford T1 </h1>'; $handle=file('ftp://user:password@eeeproject.com/public_html/test.csv'); foreach ($handle as $row) { $data= explode(",", $row); for($c=0; $c<11; $c++) { echo $data[$row][$c]; //print out each value individually } } } Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted April 3, 2014 Share Posted April 3, 2014 Your data is contained in the $data array, So if you want access column 5 and row 100 then you'd use echo $data[99][4]; // value on row 100, column 5 Quote Link to comment Share on other sites More sharing options...
jadeg Posted April 3, 2014 Author Share Posted April 3, 2014 I have tried that an I get an error undefined offset: 99 in c:\wamp\project\newav.php on line 73 Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted April 3, 2014 Share Posted April 3, 2014 (edited) My bad. I didn't read your code properly I thought the foreach loop was adding each row to $data. Back to the drawing board... What is the FTP for? If the file is on the same server as the php code then there is no need for that. Try the following $data = array(); if (($handle = fopen("test.csv", "r")) !== FALSE) { while (($row = fgetcsv($handle)) !== FALSE) { $data[] = $row; } fclose($handle); } echo $data[99][4]; Edited April 3, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
jadeg Posted April 3, 2014 Author Share Posted April 3, 2014 Thank you what is I want to get the index for each value using a variable Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted April 3, 2014 Share Posted April 3, 2014 You still can, you just need to loop through the data array foreach($data as $row) { foreach($row as $value) { echo $value; } } Quote Link to comment Share on other sites More sharing options...
jadeg Posted April 3, 2014 Author Share Posted April 3, 2014 this is what I have done so far. However I am having a problem with multiplying each cell by 0.1 as done here $data=$data2*0.1 if($login_result) { echo '<h1> Last Second Reading for Bridgetford T1 </h1>'; $handle=file('ftp://user:test@eeeproject.com/public_html/test.csv'); $data =array(); $i=0; foreach ($handle as $row) { if($i==0){ $i++; } else { $data1= explode(",", $row); $count=count($data1); for($c=0; $c<$count; $c++) { $data2[$i][$c]=$data1[$c]; $data=data2*0.1; //multiply each cell by 0.1 } $i++; } } var_dump($data); } Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted April 4, 2014 Solution Share Posted April 4, 2014 I dont understand what you are tying to do there $data=data2*0.1; //multiply each cell by 0.1 what is data2? I think you meant $data2 (dollar sign missing), in which case $data2 is an array. Doing $data2*0.1 is not possible, you cannot do an arithmetic operation on any array like that. In order to do that you need to loop through the array and apply that operation to each value in that array. The line previous is defining the values for the $data2 array. $data2[$i][$c]=$data1[$c]; So you can do multiply $data1[$c] by 0.1 here. So your for loop will be just for($c=0; $c<$count; $c++) { $data2[$i][$c] = $data1[$c] * 0.1; } $data2 array will now hold values that are 10% greater than those stored in $data1. Quote Link to comment Share on other sites More sharing options...
jadeg Posted April 19, 2014 Author Share Posted April 19, 2014 Thank you so much! That works Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.