0

My url is node/3?filter-value=67&filter-value=80 and when i try to do this code:

$param = \Drupal::request()->query->all();`

$param['filter-value']

It shows the value 80 only. I want to get both of them.

2

You’ll need to use array notation in the URL:

node/3?filter-value[]=67&filter-value[]=80

Then get(‘filter-value’) will return an array with both values.

See https://stackoverflow.com/questions/6243051/how-to-pass-an-array-within-a-query-string for more information.

| improve this answer | |
3

if you don't want to change the structure of your url.

you can use Explode function

I have made a helper function try it out:

function getParamsOfUrlWithSameKey(){
  $current_uri = \Drupal::request()->getRequestUri();
  $items = explode('?', $current_uri);
  $res_param = [];
  if (isset($items[1])) {
    $params = explode('&', $items[1]);
    foreach ($params as $param) {
      $param_value = explode('=', $param);
      $key = $param_value[0];
      $value = $param_value[1];
      $res_param[$key][] = $value;
    }
  }
  return $res_param;
}

The return of this function will be something like:

array:1 [▼
   "filter-value" => array:2 [▼
     0 => "67"
     1 => "80"
   ]
 ]
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.