Search code examples
phphtmlurlurl-encoding

Output Get Variables with multiple arrays in PHP


I am perplexed in getting the GET Variables for the url of this specific page.

The URL is :-

http://thisthat.com/category/?field_issue_month_value%5Bvalue%5D%5Byear%5D=2013&field_issue_month_value%5Bvalue%5D%5Bmonth%5D=10

I am trying to output both of these GET variables.

I tried following ways and it failed :(

echo urldecode("field_issue_month_value%5Bvalue%5D%5Bmonth%5D");
     [field_issue_month_value]
echo $_GET['field_issue_month_value[value][year][0]'];
echo $_GET["field_issue_month_value[value][month]"];

echo urldecode($_GET['field_issue_month_value%5Bvalue%5D%5Bmonth%5D']);

The Var_dump is as follows:-

{ ["field_issue_month_value"]=> array(1) { ["value"]=> array(2) { ["year"]=> string(4) "2013" ["month"]=> string(2) "10" } } ["q"]=> string(19) "Page-Name" }

Solution

  • If $_GET contains an array, you should access the data the following way:

    Your array is multidimensional, thus you need several indeces to get that data:

    $_GET['field_issue_month_value']['value']['year'] ;
    

    Otherwise using $_GET['field_issue_month_value[value][year][0]']; you just go 1-level deep, using one string as an index.


    UPD: This example works fine for me:

    parse_str(urldecode('field_issue_month_value%5Bvalue%5D%5Byear%5D=2013&field_issue_month_value%5Bvalue%5D%5Bmonth%5D=10'), $r);
    
    echo $r['field_issue_month_value']['value']['month'] . "<br/>" ;
    echo $r['field_issue_month_value']['value']['year'] . "<br/>" ;