Reputation: 731
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);
}
Upvotes: 1
Views: 1452
Reputation: 7438
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:
Upvotes: 3
Reputation: 320
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
Upvotes: 2
Reputation: 307
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;
}
}
Upvotes: 2
Reputation: 151
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().
Upvotes: 3
Reputation: 943220
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));
Upvotes: 3
Reputation: 157957
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))
Upvotes: 5