P
P
pr-Legolasov2019-08-05 17:00:30
JSON
pr-Legolasov, 2019-08-05 17:00:30

Get json string name?

Good afternoon!
I am integrating the API of the PEC transport company on my store.
I'm trying to get the id of the city by its name.
The situation is as follows: PEC provides access to a file that stores their internal ordinal numbers of cities, and which are necessary for working with their API, but the ids of these cities are entered in the file as string names, not their contents, while the content is the name of the city. Those. you need to get the name of the line knowing its contents, but not knowing the name of the line.
Everything is in the file: www.pecom.ru/ru/calc/towns.php ;
It has the following structure: 5d4831e780ecc052214609.png
I get the result for the city of Abakan in the following way: 5d4832882770f244728652.png
but an array is displayed:5d4832e789e75558939236.png
and I need to display only the name of the first line, for Abakan it is "-584988", for other cities the names are different, for each unique, because this is the same city id.
I can display the value of the first line using its name: 5d48346a7847c651701924.png
I get: 5d48347dc1eed963257425.png
but it is necessary vice versa, by the way, the line is always in the first place in the array for any city.
There are some options for obtaining, perhaps there is a way to display the name of the first row of the array?
My knowledge of json is slightly imperfect, so I could mix up the terms (array, string).
I ask for any help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vista1x, 2019-08-05
@pr-Legolasov

function findId($json_data, $cityName) {
  foreach($json_data[$cityName] as $id => $regionName) {
    if ($id < 0) return $id;
  }
}

var_dump(findId($json_data, 'Абакан'));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question