在PHP中动态构建Json [关闭]
问题描述:
I'm developing an administrative system, and it send values from html -> Js -> controller (php) -> wadl, the question is that I send values from controller to a web services in a json structure like this:
'{"idOperation":"013",
"accessLog":{"sessionId":"'.$id_sessionid.'","userName":"'.$id_username.'"},
"token":"'.$id_token.'",
"bean":"{\"CodClient\":\"'.$id_codigo.'\",
\"CodActivity\":'.$objetoJson->cod_actividad.',
\"Status\":null,
\"ShowEverything\":'.$objetoJson->id_show.'}"}'
How can I make a function in php that dynamically creates this Json?
答
function createJson ($param1, $param2, $param3, $param4, $param5, $param6 ){
$arr = array('a' => $param1, 'b' => $param2, 'c' => $param3, 'd' => $param4, 'e' => $param5);
echo json_encode($arr);
}
call the function:
$json = createJson(1, 2, 3, 4, 5);
echo $json;
and your output will be like:
{"a":1,"b":2,"c":3,"d":4,"e":5}