Save geojson value

Asked

Viewed 28 times

1

How is it that by making one geojson I could record numbers instead of string.

Example:

$geojson = array(
    'type'      => 'FeatureCollection',
    'features'  => array()
);

while( ($registo = mysqli_fetch_assoc($resultado)) !=null)
{
    $feature = array(
        'type' => 'Feature', 
        'geometry' => array(
            'type' => 'Point',
            'coordinates' => array($registo['x'], $registo['y'])
        ),
        'properties' => array(
            'id_publicidade' => $registo['id_publicidade'],
            'id_utilizador' => $registo['id_utilizador'],
            'nome' => $registo['nome'],
            'categoria' => $registo['categoria'],
        )
    );
array_push($geojson['features'], $feature);
};
echo json_encode($geojson);

My question is in the: 'coordinates' => array($registo['x'], $registo['y'])

By doing the echo of json the result is:
"coordinates":["-8.847618468261716","41.693271914384525"]

I want the quotation marks not to be read, I want the json to be without the spas, because by continuing my code the spas somehow manage to interfere with the coordinates.

  • Have you tested using the floatval()? Thus: 'coordinates' => array(floatval($registo['x']), floatval($registo['y']))

1 answer

1

You can use the floatval() that converts a string in value with fractional part:

'coordinates' => array(floatval($registo['x']), floatval($registo['y']))
                          ^                        ^
  • 1

    Perfect friend, totally unaware of this. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.