0

Using PHP, I need to take all possible GET parameters from a URL, sort them into alphabetical order and then add all with values to an array. The parameters received will not always be the same.

As an example, if I receive the following; https://example.com?PARAMA=123&PARAMB=&ANOTHERPARAM=example

It needs to go into an array like this;

$dataArray = array (
     "ANOTHERPARAM"="example",
     "PARAMA"=123
)

Ensuring items that do not have a value is not entered into the array.

Any help will be greatly appreciated.

robolist
  • 159
  • 1
  • 14

1 Answers1

0

Use the global variable $_GET. It holds all the URL params passed to the page. Then sort it using the ksort function, it sorts using the key.

 $dataArray = $_GET;
 $dataArray = array_filter($dataArray, 'strlen');
 ksort($dataArray);

For more details, check these:

Olawale
  • 741
  • 7
  • 11