How to pass an array via $_GET in php?

后端 未结 4 1614
眼角桃花
眼角桃花 2020-11-29 03:35

How can I pass one or more variables of type array to another page via $_GET?

I always passed variable values in the form ?a=1&b=2&c=3

W

4条回答
  •  情深已故
    2020-11-29 03:54

    $city_names = array(
        'delhi',
        'mumbai',
        'kolkata',
        'chennai'
    );
    $city_query = http_build_query(array('city' => $city_names));
    

    this will give you:

    city[0]=delhi&city[1]=mumbai&city[2]=kolkata&city[3]=chennai
    

    if you want to encode the brackets also then use the below code:

    $city_query = urlencode(http_build_query(array('city' => $city_names)));
    

    Output:

    city%255B0%255D%3Ddelhi%26city%255B1%255D%3Dmumbai .....
    

    Reference: http_build_query, urlencode

提交回复
热议问题