我有一个看起来像这样的功能.
function startChatSession($items) {
foreach ($test as $chatbox => $void) {
$items .= $this->chatBoxSession($chatbox);
}
if ($items != '') {
$items = substr($items, 0, -1);
}
header('Content-type: application/json');
?>
{
"username": "<?php echo $_SESSION['username'];?>",
"items": [
<?php echo $items;?>
]
}
<?php
exit(0);
}
我对这个功能的第二部分很感兴趣.我想把它翻译成像:
echo json_encode(array(
'username'=>$_SESSION['username'],
'items'=>$items
));
exit;
这种作品,但并不完全.
响应应如下所示:
{
"username": "johndoe",
"items": [{
"s": "1",
"f": "janedoe",
"m": "dqwdqwd"
}, {
"s": "1",
"f": "janedoe",
"m": "sdfwsdfgdfwfwe"
}, {
"s": "1",
"f": "janedoe",
"m": "werwefgwefwefwefweg"
}]
}
在我的情况下它看起来像:
{
"username": "johndoe",
"items": "\t\t\t\t\t {\r\n\t\t\t\"s\": \"1\",\r\n\t\t\t\"f\": \"babydoe\",\r\n\t\t\t\"m\": \"test\"\r\n\t },\t\t\t\t\t {\r\n\t\t\t\"s\": \"1\",\r\n\t\t\t\"f\": \"\",\r\n\t\t\t\"m\": \"\"\r\n\t }"
}
有任何想法吗?
谢谢
编辑:
如果我转储$items我喜欢的东西:
{
s: 1,
f: babydoe,
m: test
}, {
s: 1,
f: babydoe,
m: test
}, {
s:1,
f: babydoe,
m: test
}, {
s: 1,
f: babydoe,
m: test
}
最佳答案 试试这个:
echo json_encode(array(
'username'=>$_SESSION['username'],
'items'=>array($items)
));
你需要一个多级数组.
例如,请参见codepad.