I posted this question earlier but needed to clarify quite a few things to make it easier for everyone. I'm trying to output this with JSON and nothing I'm doing seems to be working...
{
"request": {
"act": "user_create",
"user_email": "newuser@example.com",
"user_zone": "example.com"
},
"result": "success",
"msg": "A message that will be displayed to the end user"
...
}
Here's the code that I'm currently using:
<?php
$array[0]=array("request"=>array("act"=>'user_create',
"user_email"=>"newuser@example.com",
"user_zone"=>"example.com"));
$array[1]=array('result' => 'success');
$array[2]=array('msg' => 'A message that will be displayed to the end user');
echo json_encode($array);
?>
With that code I am getting this output which is close but not quite there and isn't a valid json output. Not sure what to change but here's what I'm currently getting:
[{"request":{"act":"user_create","user_email":"newuser@example.com","user_zone":"example.com"}},{"result":"success"},{"msg":"A message that will be displayed to the end user"}]
If someone could please post an example of my fixed PHP code that would be great. I've been bouncing back and forth all for the past several hours testing out different solutions and nothing seems to be displaying exactly what I need :/ Thanks in advance!
Try with that..
$array = array(
"request" => array(
"act"=>"user_create",
"user_email"=>"newuser@example.com",
"user_zone"=>"example.com"
),
"result"=>"success",
"msg"=>"A message that will be displayed to the end user"
);
echo json_encode($array);
<?php
$array["request"]=array("act"=>'user_create',
"user_email"=>"newuser@example.com",
"user_zone"=>"example.com");
$array["result"] = 'success';
$array["msg"] = 'A message that will be displayed to the end user';
echo json_encode($array);
?>
This should work for you.
$array = array(
"request" => array(
"act" => "user_create",
"user_email" => "newuser@example.com",
"user_zone" => "example.com"
),
"result" => "success",
"msg" => "A message that will be displayed to the end user"
...
);
Just change every {}
to array()
and every :
to =>
and you get the same thing in PHP syntax.
<?php
$array[0]["request"] = array(
"act" => 'user_create',
"user_email" => "newuser@example.com",
"user_zone" => "example.com")
);
$array[0]['result'] = 'success';
$array[0]['msg'] = 'A message that will be displayed to the end user';
... etc. for other requests and results and msgs
echo json_encode($array);
?>
This is working . Whenever start array in JSON format , create array in PHP .
$array = array( // "request" array
"request" => array( // inner array
"act"=>"user_create",
"user_email"=>"newuser@example.com",
"user_zone"=>"example.com"
),
"result"=>"success",
"msg"=>"A message that will be displayed to the end user"
);
echo json_encode($array);
$array = array(
"key1" => array(
"innerkey1"=>"innerkeyvalue1",
"innerkey2"=>"innerkeyvalue2",
"innerkey3"=>"innerkeyvalue3"
),
"key2"=>"value2",
"key3"=>"value3"
);
echo json_encode($array);
see more here jsone_encode