1

I'm receiving a error message explaining the following error.

json_encode() expects at most 2 parameters, 3 given</p>

When I make the call to the json_encode function I have all three of the paramters set with accpted values.

I"m trying to figure out why this is because when I do tests on my code I get all accepted values with this function. Any thoughts? I think it 's something to do with the switch statement but I need further verifcation as well as information on what I'm doing wrong. Can someone enlighten me please?

public function output($message, $title, $status)
{
    switch ($status)
    {
        case 'Error':
            array('status' => 'Error');
            break;
        case 'Notice':
            array('status' => 'Notice');
            break;
        case 'Success':
            array('status' => 'Success');
            break;
    }
    echo json_encode($status, $title, $message);
}
4

6 回答 6

5

你可能会寻找这样的东西:

echo json_encode(array($status, $title, $message));

或者,正如其他人所建议的,像这样:

json_encode(array("status"=>$status, "title"=>$title, "message"=>$message))
于 2013-06-03T16:49:38.423 回答
3

您只能对单个数据结构进行编码。如果您要编码三位数据,则​​必须首先将它们组合成一个数据结构。例如:

echo json_encode(Array("status" => $status, "title" => $title, "message" => $message));
于 2013-06-03T16:50:24.480 回答
3

我认为您要做的是对数组进行编码?

public function output($message, $title, $status)
{
    switch ($status)
    {
        case 'Error':
            array('status' => 'Error');
            break;
        case 'Notice':
            array('status' => 'Notice');
            break;
        case 'Success':
            array('status' => 'Success');
            break;
    }
    echo json_encode(array($status, $title, $message));
}
output('messageval', 'titleval', 'statusval');

这将输出 JSON,如:

["statusval", "titleval", "messageval"]

或者还有这个:

public function output($message, $title, $status)
{
    switch ($status)
    {
        case 'Error':
            array('status' => 'Error');
            break;
        case 'Notice':
            array('status' => 'Notice');
            break;
        case 'Success':
            array('status' => 'Success');
            break;
    }
    echo json_encode(array('status'=>$status, 'title'=>$title, 'message'=>$message));
}
output('messageval', 'titleval', 'statusval');

这将输出类似于:

{"message":"messageval", "title":"titleval", "status":"statusval"}

此外,您的 switch 块不会做任何事情,因为您没有使用由 array() 生成的数组。

于 2013-06-03T16:53:47.583 回答
3

这是我的建议并且会起作用:

public function output($Message='', $Title='', $Status=''){
    #   We make sure our status is perfect.
    #   We make sure our status will always be what we want and not something different by mistake.
    #   We default to "Error".
    switch(strtoupper($Status)){
        default:
            $Status = 'error';
        break;

        case 'NOTICE':
            $Status = 'notice';
        break;

        case 'SUCCESS':
            $Status = 'success';
        break;
    }

    #   We output the content as JSON
    header('Content-Type: application/json');
    echo json_encode(array(
        'status'    => $Status,
        'title'     => $Title,
        'message'   => $Message
    ));

    #   Done - 0 mean the page end with no error (PHP error !)
    exit(0);
}

输出:

output('this is my message', 'this is my title', 'error');

{
    "status" : "error",
    "title" : "this is my title",
    "message" : "this is my message"
}

文件:

于 2013-06-03T16:54:38.700 回答
2
 public function output($message, $title, $status)
    {
        switch ($status)
        {
            case 'Error':
                array('status' => 'Error');
                break;
            case 'Notice':
                array('status' => 'Notice');
                break;
            case 'Success':
                array('status' => 'Success');
                break;
        }
        echo json_encode(array('status' => $status, 'title' => $title, 
    'message' =>$message));

    }

有关 json_encode 的更多信息,请参阅此json_encode

于 2013-06-03T17:15:19.917 回答
2

阅读, http: //php.net/manual/en/function.json-encode.php http://php.net/manual/en/control-structures.switch.php 在用 switch 做什么?什么都没有!!这是什么意思? 案例“错误”:数组(“状态”=>“错误”);

我想你想要像下面这样的东西,

public function output($message, $title, $status)
{
    switch ($status)
    {
        case 'Error':
            array('status' => 'Error');
            break;
        case 'Notice':
            array('status' => 'Notice');
            break;
        case 'Success':
            $output =  $title . $message;
            echo json_encode($output);
            break;
    }

}
于 2013-06-03T16:54:38.003 回答