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);
}
2
  • 2
    You're giving the bad parameters. see json_encode Commented Jun 3, 2013 at 16:52
  • I would suggest you use strtoupper function within the switch and your array wihtin the switch goes to no where. Commented Jun 3, 2013 at 16:52

6 Answers 6

5

You might looking for something like this:

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

or, as others suggested, like this:

json_encode(array("status"=>$status, "title"=>$title, "message"=>$message))
Sign up to request clarification or add additional context in comments.

Comments

3

Here's what I suggest and would work :

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:

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

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

Documentations:

13 Comments

I like this one as well however what is the purpose of the switch above with the strtoupper function and the fact of the header function why is it important to have those.
@KevinSmith The strtoupper function will make sure that if you write "eRroR", it will still work and become "ERROR". The switch will search for "ERROR" and not "eRroR" and will work in the end even if you do a mistake. The header is important. It tell the browser/client what kind of content is coming. Some client will not accept your server answer if the content-type is not set properly.
Is doing default: case 'ERROR': an acceptable syntax for the switch statement
@KevinSmith Yes. See php.net/manual/en/control-structures.switch.php. You can even do multiple case one after one.
I know this but I thought default takes the place of the case.
|
3

You can only encode a single data structure. If you have three bits of data that you want to encode, then you must combine them into a single data structure first. For example:

echo json_encode(Array("status" => $status, "title" => $title, "message" => $message));

Comments

3

I think what you are trying to do is encode an array?

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');

that will output JSON like:

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

or there is also this:

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');

which will output something similar to:

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

Also, your switch block will do nothing since you are not using the array produced by array().

1 Comment

Which means I could completely remove it.
2

Read, http://php.net/manual/en/function.json-encode.php http://php.net/manual/en/control-structures.switch.php what are you doing with switch?nothing!! what it means? case 'Error': array('status' => 'Error');

I think you want something like following,

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;
    }

}

3 Comments

This won't work. In case $status is not Success, it will fail. It will output content only when $status is equal to Success. If the programmer type in by mistake eRror, nothing is defaulted and corrected.
In that case perhaps you don't want json_encode, you just want to echo "Error occur"; so, i just posted to help him, he has to do exact coding.
Even with echo "Error occur";, you are breaking the thing. Reason: if you read the output, you need to read for either json and or your cusetom message.
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));

    }

For more about json_encode refer this json_encode

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.