I am trying to take info from two different two CSV files and add them to an array. What I do basically it's to open the first file, take it's content into an array as string. Then here it comes the tricky part. Both files have an ID field in common, so whenever the ID matches it has to be taken into the array.
I've tried to do this in two different ways, opening one file and within this opening the other, compare and save to the array. The other way was getting all the info from both files to two separe arrays and then find the match and get them to a third array.
Here's the code:
$handle0 = \fopen("/Data/mountain1.csv", "r");
if ($handle0) {
$line0 = 0;
while (($buffer0 = fgets($handle0, 4000)) !== false) {
if ($line0 > 0){
$mountainArray = str_getcsv($buffer0, ",");
$obj = array();
$obj["id"] = $mountainArray[2];
$obj["name"] = $mountainArray[0];
$obj["country"] = $mountainArray[1];
$handle1 = fopen("/Data/mountain1.csv", "r");
if ($handle1) {
$line1 = 0;
while (($buffer1 = fgets($handle1, 4000)) !== false) {
if ($line1 > 0) {
$latlonArray = str_getcsv($buffer1, ",");
$content = array();
$content["id"] = $latlonArray[1];
if ((int)$content["id"] == (int)$obj["id"]) {
$obj["latitude"] = $latlonArray[7];
$obj["longitude"] = $latlonArray[8];
}
$line1++;
}
}
fclose($handle1);
}
$mountain[] = $obj;
}
$line0++;
}
fclose($handle0);
}
This code just loops and does nothing
if ($handle0) {
while (($buffer0 = fgets($handle0, 4000)) !== false) {
$mountainArray = str_getcsv($buffer0, ",");
$content0 = array();
$content0["id"] = $mountainArray[2];
$content0["name"] = $mountainArray[0];
$content0["country"] = $mountainArray[1];
$mountain[] = $content0;
}
fclose($handle0);
}
if ($handle1) {
while (($buffer1 = fgets($handle1, 4000)) !== false) {
$latlonArray = str_getcsv($handle1, ",");
$content1 = array();
$content1["id"] = $latlonArray[1];
$content1["latitude"] = $latlonArray[7];
$content1["longitude"] = $latlonArray[8];
$latlon[] = $content1;
}
fclose($handle1);
}
foreach ($mountain as $row0) {
$obj = array();
$obj["id"] = $row0["productUid"];
$obj["name"] = $row0["name"];
$obj["country"] = $row0["address"];
foreach ($latlon as $row1) {
if((int)$row1["id"] == (int)$row0["id"]) {
$obj["latitude"] = $row1["latitude"];
$obj["longitude"] = $row1["longitude"];
}
}
$mountains[] = $obj;
}
and this one just returns null to me...