Why does only one PHP Script work while the other gives an error only looping over last item of array?

I am trying to create a simple too to get WalkScores for various coordinate pairs using the API. I have two different UI. One has two input text areas, one for the lats and one for lons. It works fine but this is more work for the user. The second UI has each coordinate pair on each line of a text area. The first script below works while the second gives errors even through they use the same functions.

Working script. This takes two fields from a form and creates the arrays to use in the loop. This works but not idea.

<?
$apiKey = 'a55fc4524343bfc9cdd901361092197c';
//comes from form
$lat = $_POST['lat'];
$lon = $_POST['lon'];

// for testing without UI
// $lat = "41.767679,41.346483";
// $lon = "-72.680463,-73.251384";
// for testing without UI

$lats = explode(',', $lat);
$lons = explode(',', $lon);

$address = 'unknown';

for ($i = 0; $i < count($lats); $i++) {
    $json = getWalkScore($lats[$i], $lons[$i], $address);

    $json_walkScore = json_decode($json);

    echo $lats[$i] . "\r\n";
    echo $lons[$i] . "\r\n";
    echo $json_walkScore->walkscore . "\r\n";
    echo $json_walkScore->description . "\r\n";
}

function getWalkScore($lat, $lon, $address)
{
    $address = urlencode($address);
    $url = "http://api.walkscore.com/score?format=json&address=$address&lat=$lat&lon=$lon&wsapikey=a55fc4524343bfc9cdd901361092197c";
    $str = @file_get_contents($url);
    return $str;
}

Not working This looks to create identical arrays for use in the loop but returns an error "Notice: Trying to get property 'walkscore' of non-object in..." and only returns the expected output for the last iteration. It works fine for one pair. If I use the hardcoded arrays it works fine..

<?
$apiKey = 'c93ad8c6751f03a31889414b680eec49';

//Data sent from Ajax
$input = $_POST['input'];

//Split string based on new line from text area. Each item is on its own line.
$arr = explode("\n", $input);

//Two arrays to hold data
$lats = [];
$lons = [];

//Break each pair into lat lng and push to arrays
foreach ($arr as $k => $v) {
    $coord = explode(",", $v);
    $lat = $coord[0];
    $lon = $coord[1];
    array_push($lats, $lat);
    array_push($lons, $lon);
}

//using these hardcoded arrays it works fine.
// $lats = ["41.113676","41.104206"];
// $lons = ["-73.439233","-73.404587"];
//using these hardcoded arrays it works fine.


//Set address to unknown since it is not provided / needed
$address = 'unknown';

//loop over the number of lats, run the get walk score function to return the json data for output
for ($i = 0; $i < count($lats); $i++) {
    $json = getWalkScore($lats[$i], $lons[$i], $address);

    $json_walkScore = json_decode($json);
    // print_r($json_walkScore);

    echo $lats[$i] . "\r\n";
    echo $lons[$i] . "\r\n";
    echo $json_walkScore->walkscore . "\r\n";
    echo $json_walkScore->description . "\r\n";
}

function getWalkScore($lat, $lon, $address)
{
    $address = urlencode($address);
    $url = "http://api.walkscore.com/score?format=json&address=$address&lat=$lat&lon=$lon&wsapikey=a55fc4524343bfc9cdd901361092197c";
    $str = @file_get_contents($url);
    return $str;
}


Any ideas on why the first is working but the second is not? Let me know if you have questions. Thanks!

Answer

Solution:

After more testing this line

$arr = explode("\n", $input);

needs to be changed to this

$arr = explode("\r\n", $input);

fun... Same EXACT output but it the code decides to work this time.

Source