如何在PHP中将json对象解析为url参数字符串?

时间:2016-03-10 23:53:34

标签: php json

我有以下json编码对象:

render... and return

我希望将其转换为url字符串,以便我可以将它与我的REST API函数一起使用,例如像这样:

{"username":"my_username","email":"my_email","password":"12345678","confirm_password":"12345678"}

我在javascript中发现了许多函数来执行此操作,但我无法在PHP中找到任何内容。 PHP的功能是什么?

1 个答案:

答案 0 :(得分:0)

以下查询字符串:

?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

相关问题