Jump to content

multidimensional array


jadeg

Recommended Posts

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:[email protected]/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	
				}

			}
			

		
    }
Link to comment
https://forums.phpfreaks.com/topic/287492-multidimensional-array/
Share on other sites

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];

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:[email protected]/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);
				
    }

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.

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