There is a json string with a lot of data, which I decoded into a lot of arrays using json_decode. Each array has a name field. For example, ["name"]=> string(5) "Seoul" . How can I search for the "name" field with the Seoul key? And if before this field there is a field ["code"]=> string(3) "SEL" , how can I parse it?

Closed due to the fact that the essence of the issue is not clear to the participants of the korytoff , Dmitriy Simushev , rjhdby , Kirill Stoianov , HamSter October 13 '16 at 5:41 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

    2 answers 2

    For starters, 'Seoul' is not a key, but a value. The keys in this case are 'name', 'code'. You can access the value by key as follows: $array['name'] . How to find an array with a specific key value name? Cycle through all, check the meaning of each.

    The expression "before this field" is also not particularly correct. All array keys are equal, although php supports the original order. So, just take and refer to the value of the array element by the key code.

    • First comes the key "code", and after it comes the key "name". I need to find in which array the value of "Seoul" is located, and then, parse the value of the key "code" from the same array. - JamesJGoodwin
    • They only go "first" and "after" in the text. In the parsed array, they are equal. You already have a finished array, you do not need to parse it again. Find the array with the desired name value in the loop and take code from it. I will not write the code for you - try to do it yourself, otherwise you will never be able to learn the language. - Yury Sitnikov
    • one
      Wrote the code. You can be proud of me :) - JamesJGoodwin

    Found in the vast StackOverfow a wonderful function of finding the number of a multidimensional array (or how correctly is it called?):

     function searcharray($value, $key, $array) { foreach ($array as $k => $val) { if ($val[$key] == $value) { return $k; } } return null; } 

    Using this function, I pulled out the number of the array and already having the number of the array removed the value of the key code from it:

     $cities_json = file_get_contents('json/cities.json'); $data = json_decode($cities_json, true); $key = searcharray($geo['city']['name_en'], 'name', $data); $origin = $data[$key]['code']; // IEV