Storing a parameter in an array in a URL as a variable in PHP

Viewed 33

Here is the URL:

https://all.burgbuilderdev.com/results-page/?address%5B0%5D=Milwaukee%2C%20WI%2053207&post%5B0%5D=project&distance=30&units=imperial&per_page=5&lat=42.971207&lng=-87.904057&form=2&action=fs

I realize it is in an array but I can't figure out how to target it and store it as a variable. I'm certain this is dumb (I'm a beginner with PHP) but I've tried things like:

$address = $_GET['address'];

and

$address = $_GET ['address%5B0%5D'];

and

$address = $_GET['address[0]'];

to no avail.. Thanks for stopping by!

1 Answers

Given:

$url = 'https://all.burgbuilderdev.com/results-page/?address%5B0%5D=Milwaukee%2C%20WI%2053207&post%5B0%5D=project&distance=30&units=imperial&per_page=5&lat=42.971207&lng=-87.904057&form=2&action=fs';

We can parse it like PHP automatically does:

$parsed = parse_url($url);

parse_str($parsed['query'], $parsedQuery);

var_dump(
    $parsed,
    $parsedQuery,
    $parsedQuery['address'][0]   // <<< Here is like $_GET
);

Renders (partial):

array(9) {
  ["address"]=>                       // <<< address key
  array(1) {                          // <<< Address is array w/one item
    [0]=>                             // <<< First item, zero index
    string(19) "Milwaukee, WI 53207"  // <<< Here
  }
  ["post"]=>
  array(1) {
    [0]=>
    string(7) "project"
  }
  ["distance"]=>
  string(2) "30"
...

https://3v4l.org/S7X7L

So in other words, PHP automatically does the above for GET, so it would be:

$_GET['address'][0]
Related