J
J
jammy2018-09-05 15:05:23
PHP
jammy, 2018-09-05 15:05:23

How to represent string from mysql to json without quotes?

Help the teapot please. I can't think of anything at all.
I add markers to Yandex map using objectManager (using json)
The coordinates of objects in the database are a string (for example: 55.722012, 37.183899)
I collect JSON using json_encode Naturally,
Yandex API does not want to understand the "geometry":{"type":"Point", "coordinates":"[55.722012, 37.183899]"}
since the coordinates must be an array "geometry":{"type":"Point","coordinates":[ 55.722012 , 37.183899]}
I really don't want to make changes in mysql. Is it possible to come up with a crutch when assembling json?
Short code:

while ($row = mysqli_fetch_array($result))
    {
        $geometry[] = array(
            'type'    => 'Point',
            'coordinates' => '['.$row['y_coordinates'].']'
            );
        $employee_data[] = array (
            'geometry'   =>   $geometry
            );
    }
    json_encode($employee_data, JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2018-09-05
@jammy

Probably, it is necessary not to put a string, but to put an array in coordinates. Try replacing it with something like A if you want the array elements to be numbers:

array_map('floatval', explode(', ', $row['y_coordinates']))

K
krypt3r, 2018-09-05
@krypt3r

'coordinates' => '['.$row['y_coordinates'].']'
Thus, you created not an array, but a string framed in square brackets.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question