根据某些情况,我很少有状态返回到json_encode对象。
if(verify == 1)
$data = array('status'=>'SUCCESS', 'points'=>$points, 'user'=>$user);
if(verify == 2)
$data = array('status'=>'INACTIVE');
if(verify == 0)
$data = array('status'=>'FAILED');
$data_str = json_encode($data);
我需要$data_str
添加为查询字符串,当它重定向到hitter网址时,例如:https://www.example.com/members?status=SUCCESS&points=2500&user=albert@hotmail.com
或https://www.example.com/members?status=INACTIVE
如何将$data_str
作为查询字符串传递?
答案 0 :(得分:3)
您可以使用PHP
函数http_build_query
来实现此功能,您永远不需要使用其他任何内容,例如foreach
循环:
if($verify == 1)
$data = array('status'=>'SUCCESS', 'points'=>$points, 'user'=>$user);
if($verify == 2)
$data = array('status'=>'INACTIVE');
if($verify == 0)
$data = array('status'=>'FAILED');
$url = https://www.example.com/members?.http_build_query($data);
修改强>
这是demo
答案 1 :(得分:0)
答案 2 :(得分:0)
试试这个:
$verify = 1;
$points = 2500;
$user = 'albert';
if($verify == 1)
$data = array('status'=>'SUCCESS', 'points'=>$points, 'user'=>$user);
if($verify == 2)
$data = array('status'=>'INACTIVE');
if($verify == 0)
$data = array('status'=>'FAILED');
//$data_str = json_encode($data);
$qryStr = array();
foreach($data as $key => $val){
$qryStr[] = $key."=".$val;
}
echo $url = 'https://www.example.com/'.implode("&", $qryStr); //https://www.example.com/status=SUCCESS&points=2500&user=albert
或使用http_build_query()
。
echo $url = 'https://www.example.com/'.http_build_query($data); //https://www.example.com/status=SUCCESS&points=2500&user=albert