C
C
Chesterfield252022-01-03 18:04:44
PHP
Chesterfield25, 2022-01-03 18:04:44

How to return in json?

There is a php code that needs to be returned in json format, how to do it?

echo('city: '.$city['city']['name_en'].'<br>');
echo('city code: '.$city['city']['id'].'<br>');
echo('city lat: '.$city['city']['lat'].'<br>');
echo('city lon: '.$city['city']['lon'].'<br>');
echo('country: '.$city['country']['name_en'].'<br>');
echo('country iso: '.$city['country']['iso'].'<br>');
echo('country code: '.$city['country']['id'].'<br>');


How to make transfers?
$arr = array('IP' => $ip,
             'city' => $city['city']['name_en'],
             'city code' => $city['city']['id'],
             'city lat' => $city['city']['lat'],
             'city lon' => $city['city']['lon'],
             'country' => $city['country']['name_en'],
             'country iso' => $city['country']['iso'],
             'country code' => $city['country']['id'],);

echo json_encode($arr);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Daria Motorina, 2022-01-03
@Chesterfield25

You can use the JSON_PRETTY_PRINT
JSON constants flag

$arr = array('IP' => $ip,
             'city' => $city['city']['name_en'],
             'city code' => $city['city']['id'],
             'city lat' => $city['city']['lat'],
             'city lon' => $city['city']['lon'],
             'country' => $city['country']['name_en'],
             'country iso' => $city['country']['iso'],
             'country code' => $city['country']['id'],);

echo json_encode($arr, JSON_PRETTY_PRINT);

sandbox

A
Artem Prokhorov, 2022-01-03
@kotcich

file_put_contents(path, json_encode(array));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question