How to handle such a link?

https://auto.yandex.ru/offers?sort_offers=relevance-desc&mark=audi&mark=toyota&mark=bmw

The link sent several brands with different values ​​and the keys are the same, how to handle this?

I tried http: //test.loc/? r = 1 & r = 2 & r = 3

print_r($_GET['r'])// результат 3 

and how to get all the values?

  • one
    why did you decide that Yandex gets its values ​​from a URL for PHP? - MasterAlex
  • one
    no matter what, it is important how they decided - bemulima

5 answers 5

this way, form the link differently ... you can parse the URL as a string and pull all the matches - but this is very ugly and crutch. You can get the entire query string in $ _SERVER ['QUERY_STRING'] and already parse it ...

  • one
    Do you think Yandex decided so, parsil url string? - bemulima 2:41 pm
  • one
    I don’t know, but you don’t get to all r through the $ _GET array - Boris Runs
  • @ Boris, I think Yandex does not write in PHP ... - Roman Grinyov
  • I am more than sure about this) - Boris Runs
 $ url = parse_url ('https://auto.yandex.ru/offers?sort_offers=relevance-desc&mark=audi&mark=toyota&mark=bmw');
 if (preg_match_all ("/ (? P [\ w \ - \ _] +) = (? P [^ = &] +) / isu", $ url ['query'], $ matches, PREG_SET_ORDER)) {
     $ url ['query'] = [];
     foreach ($ matches as $ item) {
         $ item ['value'] = urldecode ($ item ['value']);
         if (! isset ($ url ['query'] [$ item ['key']])) {
             $ url ['query'] [$ item ['key']] = $ item ['value'];
             continue;
         }

         if (! is_array ($ url ['query'] [$ item ['key']])) {
             $ url ['query'] [$ item ['key']] = [$ url ['query'] [$ item ['key']], $ item ['value']];
         } else {
             $ url ['query'] [$ item ['key']] [] = $ item ['value'];
         }
     }
 }
 var_dump ($ url);
 / **
 array (4) {
   ["scheme"] =>
   string (5) "https"
   ["host"] =>
   string (14) "auto.yandex.ru"
   ["path"] =>
   string (7) "/ offers"
   ["query"] =>
   array (2) {
     ["sort_offers"] =>
     string (14) "relevance-desc"
     ["mark"] =>
     array (3) {
       [0] =>
       string (4) "audi"
       [1] =>
       string (6) "toyota"
       [2] =>
       string (3) "bmw"
     }
   }
 }
 ** /
  • It may be necessary to fix the regular season, but it seems to me that it should work. - ilyaplot
  • In general, Yandex generates an invalid URL. Of course, it looks prettier than mark [] = audi & mark [] = ..., but I am not in favor of such decisions. - ilyaplot

if it is possible to change the programming language for the implementation of this task, then use for example python + Flask to get the het parameters like this:

 request.args.getlist('r') 

    Like so

     foreach($_GET as $key => $value){ echo $key." = ".$value."</br>"; } 

    But all the last value alone will remain

    • true, but how did Yandex solve this question? - bemulima
    • Apparently the same way as the colleague Boris described. - rjhdby 2:41 pm
    • 2
      @bemulima $ _SERVER ['REQUEST_URI'] for example - rjhdby 2:42 pm
    • interesting, very closely answered - bemulima

    So try, you should put in the array $ _GET ['r'] all 3 values

    http: //test.loc/? r [] = 1 & r [] = 2 & r [] = 3

    • The source line is not generated by the author, he needs to parse the incoming data - ilyaplot