Hi Guys,
I’m trying to do the following:
- Get data from 2 different CSV files (already managed to do this)
- load the data into 2 seperate arrays (already managed to do this)
- remove the data that i dont need, as I only want to use column 2 and 3 (already managed to do this)
- compare the data from column 1 of each file - if they are the same then add the integer values of column 2 of each file together for each row. (NOT managed to do this yet)
- create a third array with all the new values (NOT managed to do this yet)
So far I have manged to create the 2 arrays but I dont know how to compare the 0 value of each key and compare to the other array’s 0 value and if they match add the 1 key values of the 2 arrays. Hope this makes sense. Basically i want to add 7415 with 3452 for date 110227 and so on and then put all the new values with their dats in a new array so i can write it to CSV.
here are the arrays:
Array ([0] => Array([0] => 110227 [1] => 7415)
[1] => Array([0] => 110228 [1] => 9102)
[2] => Array([0] => 110301 [1] => 7693)
)
Array([0] => Array([0] => 110227 [1] => 3452)
[1] => Array([0] => 110228 [1] => 4223)
[2] => Array([0] => 110301 [1] => 3422)
)
Here’s my code so far:
<?php function loadData($dataSource){ //Declare the new variable as an array $CSV = array(); //Open the CSV file if (($arData = fopen($dataSource, "r")) !==FALSE){ // Set the parent array key to 0 $key = 0; // While there is data available loop through unlimited times (0) using separator (;) while (($data = fgetcsv($arData, 0, ";")) !==FALSE) { // Count the total keys in each row $c = count($data); //Populate the array for ($x=0;$x<$c;$x++) { if($x<2) { $CSV[$key][$x] = $data[$x]; } } $key++; } // end while // Close the CSV file fclose($arData); } // end if return $CSV; } //The CSV files have 15 columns and I'm just slicing it to 3 for testing purposes $array1 = array_slice(loadData("src/data1.csv"),1,3); $array2 = array_slice(loadData("src/data2.csv"),1,3); echo ""; print_r($array1); echo ""; echo "
"; echo "
"; print_r($array2); echo ""; ?>
Unfortunately i’m very new to this complicated thing called programming and I would really apprciate any help if possible.
Thanks in advance…