reemy reemy - 2 months ago 15
JSON Question

JSON PHP foreach empty value

I'm working with PHP and JSON. I have this example of a JSON foreach loop, with this example I want to get lat/lon to add a marker in Google Maps:

"data": [
{
"end_time": "2016-02-09T22:30:00-0200",
"name": "6\u00ba Rebanh\u00e3o de Carnaval - Comunidade Cen\u00e1culo",
"place": {
"name": "Comunidade Cen\u00e1culo",
"location": {
"city": "Franca",
"country": "Brazil",
"latitude": "",
"longitude": "",
"state": "SP",
"street": "Dom Pedro I, 1040, Jardim Petr\u00e1glia"
},
"id": "410657548946610"
},
"start_time": "2016-02-05T19:30:00-0200",
"id": "940527599373114"
}
]


My problem is, some of results of latitude and longitude are empty. I want to put a specific value on it, but on each empty I want to add lat + 0.02.

For example, in first empty value I want to put this lat 40.700106, and the next empty value I want to put this 40.700106 + 0.02.

I just don't want 2 markers in same location.

How can I do this?

EDIT:

foreach($json3 ->data as $mydata1)
{
$latitude = $mydata1->place->location->latitude;
if(empty($latitude ))
{

$latitude = $latitude + 0.02;
echo ' lat :'.$latitude ;
}
}


this way i replace the empty value, but they dont add the + 0.02 on each value

Answer

You can save the previous latitude and use it.

Something like this:

$previousLatitude = null;
foreach($json3 ->data as $mydata1) {
    $latitude = $mydata1->place->location->latitude;
    if(empty($latitude ))
    {
        $latitude = $previousLatitude + 0.02;
        echo ' lat :'.$latitude ;
    }

    $previousLatitude = $latitude
}