Jump to content

multidimensional array


Go to solution Solved by Ch0cu3r,

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: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	
				}

			}
			

		
    }
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];
Edited by Ch0cu3r

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);
				
    }
  • Solution

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