开发者

is this valid json array using php

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


This is called a heredoc. It works as though all the text inside is a single string. Do not use it if you are trying to generate JSON. Instead, use the fact that the statement is not done until it hits a semicolon:

$somevar = {
  "s": "1",
  "f": "{$to}",
  "m": "{$messagesan}"
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜