Passing arrays as url parameter

前端 未结 10 1903
礼貌的吻别
礼貌的吻别 2020-11-22 12:32

What is the best way that I can pass an array as a url parameter? I was thinking if this is possible:

$aValues = array();

$url = \'http://www.example.com?a         


        
10条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 13:00

    There is a very simple solution: http_build_query(). It takes your query parameters as an associative array:

    $data = array(
        1,
        4,
        'a' => 'b',
        'c' => 'd'
    );
    $query = http_build_query(array('aParam' => $data));
    

    will return

    string(63) "aParam%5B0%5D=1&aParam%5B1%5D=4&aParam%5Ba%5D=b&aParam%5Bc%5D=d"
    

    http_build_query() handles all the necessary escaping for you (%5B => [ and %5D => ]), so this string is equal to aParam[0]=1&aParam[1]=4&aParam[a]=b&aParam[c]=d.

提交回复
热议问题