I've been searching and reading similar questions, but not finding anything that is similar.
I am creating an array from an sql query for sample data. In that sample data there are geo coordinates, and region definitions. The problem is that the ge coordinates aren't always populated. But the region is. So I want to create a rule set that defines default geo coordinates for each region. Then, an if loop that would fill in those coordinates based on the region identifier if the latitude was NULL.
I don't have any developing code, because I'm not even sure where to start with changing PHP arrays. Forgive me if the way I am referring to the arrays below is confusing.
PHP
<?php
$host = "555.555.55.55";
$user = "user";
$pass = "password";
$db = 'schema';
$mysqli = new mysqli($host, $user, $pass, $db);
if ($mysqli->connect_error)
{
echo "Can't connect: $mysqli->errno() $mysqli->error()";
}
$sql = "select * from Sample_Data";
if ($res = $mysqli->query($sql))
{
$rows = array();
while ($row = $res->fetch_assoc())
{
$rows[] = $row;
}
}
print("<pre>".print_r($rows,true)."</pre>");
//echo json_encode($rows, JSON_PRETTY_PRINT);
?>
BROWSER
Array
(
[0] => Array
(
[ID] => 1
[File_Name] => b0517
[Sample_Number] => B0517
[Date] => 2017-05-23
[Time] =>
[Lattitude] =>
[Longitude] =>
[Location_Description] => Blank 5/17
[Sample_Type] => Water
[Sample_Type_Description] => Blank
[Classification] => Blank
[Classification_Description] => Sample Blank
[Depth_Taken] => 0
[Volume_Filtered] => 100
[Region] => GoM
[Processing_Lab] => UCB
[QC_Code] =>
[QC_Description] =>
[Colilert_Results] =>
)
[1] => Array
(
[ID] => 2
[File_Name] => d1
[Sample_Number] => D1
[Date] => 2017-01-18
[Time] => 11:03:00
[Lattitude] => 30.42357
[Longitude] => -89.07170
[Location_Description] => Rippy
[Sample_Type] => Water
[Sample_Type_Description] => Freshwater
[Classification] => Unknown
[Classification_Description] => Unknown Sample
[Depth_Taken] => 0
[Volume_Filtered] => 100
[Region] => GoM
[Processing_Lab] => UCB
[QC_Code] =>
[QC_Description] =>
[Colilert_Results] =>
)
[2] => Array
(
[ID] => 3
[File_Name] => d2
[Sample_Number] => D2
[Date] => 2017-01-18
[Time] => 12:12:00
[Lattitude] =>
[Longitude] =>
[Location_Description] => Ohio
[Sample_Type] => Water
[Sample_Type_Description] => Freshwater
[Classification] => Unknown
[Classification_Description] => Unknown Sample
[Depth_Taken] => 0
[Volume_Filtered] => 100
[Region] => R6
[Processing_Lab] => UCB
[QC_Code] =>
[QC_Description] =>
[Colilert_Results] =>
)
And so on for 500 groups...
DESIRED CRITERIA
[Another array?]
[R6] = [Lattitude] => 55.5555
[Longitude] => 55.5555
[GOM]= [Lattitude] => 66.6666
[Longitude] => 66.6666
something like....
foreach [i][Lattitude] in $rows
if [i][Lattitude] == NULL
Then compare [i][Region] to the array above and replace [Lattitude] and [Longitude]
RESULT
Array
(
[0] => Array
(
[ID] => 1
[File_Name] => b0517
[Sample_Number] => B0517
[Date] => 2017-05-23
[Time] =>
[Lattitude] => 66.6666
[Longitude] => 66.6666
[Location_Description] => Blank 5/17
[Sample_Type] => Water
[Sample_Type_Description] => Blank
[Classification] => Blank
[Classification_Description] => Sample Blank
[Depth_Taken] => 0
[Volume_Filtered] => 100
[Region] => GoM
[Processing_Lab] => UCB
[QC_Code] =>
[QC_Description] =>
[Colilert_Results] =>
)
[1] => Array
(
[ID] => 2
[File_Name] => d1
[Sample_Number] => D1
[Date] => 2017-01-18
[Time] => 11:03:00
[Lattitude] => 30.42357
[Longitude] => -89.07170
[Location_Description] => Rippy
[Sample_Type] => Water
[Sample_Type_Description] => Freshwater
[Classification] => Unknown
[Classification_Description] => Unknown Sample
[Depth_Taken] => 0
[Volume_Filtered] => 100
[Region] => GoM
[Processing_Lab] => UCB
[QC_Code] =>
[QC_Description] =>
[Colilert_Results] =>
)
[2] => Array
(
[ID] => 3
[File_Name] => d2
[Sample_Number] => D2
[Date] => 2017-01-18
[Time] => 12:12:00
[Lattitude] => 55.5555
[Longitude] => 55.5555
[Location_Description] => Ohio
[Sample_Type] => Water
[Sample_Type_Description] => Freshwater
[Classification] => Unknown
[Classification_Description] => Unknown Sample
[Depth_Taken] => 0
[Volume_Filtered] => 100
[Region] => R6
[Processing_Lab] => UCB
[QC_Code] =>
[QC_Description] =>
[Colilert_Results] =>
)