is this valid json array using php
Posted
by Rich
on Stack Overflow
See other posts from Stack Overflow
or by Rich
Published on 2010-03-30T01:26:04Z
Indexed on
2010/03/30
1:33 UTC
Read the original article
Hit count: 425
Hello,
I need to convert some code done by someone else, to work in my mvc model
It is using some functions like EOD that I don't understand. Does that still work in a class?
Primarely, my question focusus on the json output.
The old code does not use the php json_encode function, but outputs it directly like this
?>
{
"username": "<?php echo $_SESSION['username'];?>",
"items": [
<?php echo $items;?>
]
}
<?php
I would do it like this, but I need to be sure it's right for the items part
header('Content-type: application/json');
$output = array("username"=> isset( $_SESSION['username'] ) ? $_SESSION['username'] : "?",
"items"=>$items
);
$this->content = json_encode($output);
This is some background on how the $items is made. An item is stored like this:
$_SESSION['chatHistory'][$_POST['to']] .= <<<EOD
{
"s": "1",
"f": "{$to}",
"m": "{$messagesan}"
},
EOD;
and it is put in the $items variable like this
$items = '';
if ( !empty($_SESSION['openChatBoxes'] ) ) {
foreach ( $_SESSION['openChatBoxes'] as $chatbox => $void ) {
$items .= $this->chatBoxSession($chatbox);
}
}
//The chatBoxSession() function takes an item from the $_SESSION['chatHistory'] array and returns it.
I hope this was somewhat clear enough? The php manual warns that in some cases you don't get an array output, instead you get an object. So, with the EOD syntax, I am not really sure.
It could save me some time if I know some things are doing what they supposed too, and giving the right output.
thanks, Richard
© Stack Overflow or respective owner