Thowzif
Thowzif

Reputation: 51

GET values from variables with [] in url

I have a URL like this

?custom[weight]=1&custom[weight2]=2

If i use echo $_GET[custom[weight]] then it doesn't work. How do i retrive this value?

Upvotes: 0

Views: 44

Answers (2)

LF-DevJourney
LF-DevJourney

Reputation: 28529

You should access it with $_GET['custom']['weight'] and $_GET['custom']['weight2']

You can check it with print_r($_GET) and you'll get something like,

Array
(
    [custom] => Array
        (
            [weight] => 1
            [weight2] => 2
        )
)

Upvotes: 1

user3783243
user3783243

Reputation: 5224

In the provided example $_GET becomes multidimensional and custom is an index with the array so you want.

foreach( $_GET['custom'] as $index => $value) { 
     echo $index . ' has the value of ' .  $value . PHP_EOL; 
}

Upvotes: 1

Related Questions