我有以下json编码对象:
{"username":"my_username","email":"my_email","password":"12345678","confirm_password":"12345678"}
我想将其转换为 url 字符串,以便我可以将其与我的 REST API 函数一起使用,例如:
search?search=asdadd%2C+United+Kingdom&type=tutor
我在javascript中找到了许多函数来做到这一点,但我在PHP中找不到任何东西。PHP 中的函数是什么?
以下查询字符串:
?username=my_username&email=my_email&password=12345678&confirm_password=12345678
.. 将变成:
{"username":"my_username","email":"my_email","password":"12345678","confirm_password":"12345678"}
如果您使用 json_enconde
.
要逆转该过程,您需要使用 json_decode
以及 http_build_query
.
首先,将 JSON 转换为具有 json_decode
的关联数组:
$json = '{"username":"my_username","email":"my_email","password":"12345678","confirm_password":"12345678"}';
$associativeArray = json_decode($json, true);
现在将http_build_query
与我们构建的新关联数组一起使用:
$queryString = http_build_query($associativeArray);
结果:username=my_username&email=my_email&password=12345678&confirm_password=12345678
.