0

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] => 
            )

1 Answer 1

3

Define an array of region's default latitudes and longitudes first.

<?php

$regions = [
  'R6' => ['Lattitude' => 55.5555, 'Longitude' => 55.5555],
  'GOM' => ['Lattitude' => 66.6666, 'Longitude' => 66.6666],
];

foreach ($rows as &$row){
  if (!$row['Lattitude']){
     $row['Lattitude'] = $regions[$row['Region']]['Lattitude'];
     $row['Longitude'] = $regions[$row['Region']]['Longitude'];
  }
}

(Edited to add ampersand for $row in order to update original array)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! One modification though. You need to add an "&" before $row in "$rows as $row" so it reads "$rows as &$row". I couldn't figure out why the values wouldn't update. After some searching I found that the ampersand lets the original array get updated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.