首頁  >  文章  >  後端開發  >  php怎麼將陣列轉為url參數?

php怎麼將陣列轉為url參數?

青灯夜游
青灯夜游原創
2020-08-25 11:23:405847瀏覽

在PHP中,可以使用內建的「http_build_query()」函數將陣列轉為url參數。 「http_build_query()」函數是php5加入的,作用是把陣列或物件轉換成url參數,產生一個經過「URL-encode」的請求字串。

php怎麼將陣列轉為url參數?

推薦:《PHP影片教學

php使用http_build_query()函數將陣列轉為url參數

 'bar',
    'baz' => 'boom',
    'cow' => 'milk',
    'php' => 'hypertext processor'
);

echo http_build_query($data) . "\n";
echo http_build_query($data, '', '&');

?>

輸出結果:

foo=bar&baz=boom&cow=milk&php=hypertext+processor
foo=bar&baz=boom&cow=milk&php=hypertext+processor

http_build_query()函數介紹

http_build_query()函數的作用是使用給定的關聯(或下標)陣列產生一個經過URL-encode 的請求字串。

寫法格式:

http_build_query ( mixed $query_data [, string $numeric_prefix [, string $arg_separator [, int $enc_type = PHP_QUERY_RFC1738 ]]] )

打個比方:

$data = array("name"=>"callback" , "value"=>"test");
$rescult = http_build_query($data);

我們輸出下$rescutl可以得到:

name=callback&value=test

這個有什麼用呢,這是模擬http請求的,把得到的資料data經過函數URL-encode,一般是用在回調。

以上是php怎麼將陣列轉為url參數?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn