PHP arrays be outputed to JSON format
PHP arrays
$grades = array( array( name => "tom",
grade => 'A'
),
array( name => "jeff",
grade=> 'B'
),
array( name => "lisa",
开发者_JAVA百科 grade => 'C'
)
);
$output=array
( 'status'=>'ok',
'content'=>$grades
)
And the final JSON output I want to see would be {"status":'ok',"content":"{"tom":"A","jeff":"B","lisa":"B"}"}
The question is how should I manipulated the array so it can give me the final output like above JSON?
would json_encode($output); echo $output
do the trick?
or
json_encode($grades)
then another json_encodes($output)
but I am seeing extra \
thing like {"status":1,"content":"{\"tom\":\"A\",\"jeff\":\"B\",\"lisa\":\"B\"}"}
Try this simple command
$my_array = [
"test" => "value",
"another" => [
"fruits" => ["Apple", "Orange"]
]
];
print_r(json_encode($my_array));
It will produce
{
"test":"value",
"another":{
"fruits":["Apple","Orange"]
}
}
I don't understand why you don't write just this:
<?php
$grades = array(
array(
'name' => 'tom',
'grade' => 'A'
),
array(
'name' => 'jeff',
'grade' => 'B'
),
array(
'name' => 'lisa',
'grade' => 'C'
),
);
$output = array(
'status' => 'ok',
'content' => $grades,
);
print_r(json_decode(json_encode($output)));
It prints:
stdClass Object
(
[status] => ok
[content] => Array
(
[0] => stdClass Object
(
[name] => tom
[grade] => A
)
[1] => stdClass Object
(
[name] => jeff
[grade] => B
)
[2] => stdClass Object
(
[name] => lisa
[grade] => C
)
)
)
Isn't is the expected result?
Don't call json_encode($grades)
before json_encode($output)
, unless you have some unmentioned reason to want the grades as a string literal instead of json object. json_encode()
will recursively encode all child arrays inside $output
, there's no need to encode them separately.
As you may have figured out, an array's associative keys get represented as keys in the resulting JSON object, e.g. {key1: value1, ...}, as clearly demonstrated from the output you got.
Secondly, an array containing other arrays will be represented as a list in JSON, e.g. [...]
I believe your earlier code should looked something like this:
$grades = array(
array('name' => 'tom', 'grade' => 'A'),
array('name' => 'jeff', 'grade' => 'B'),
array('name' => 'lise', 'grade' => 'C'),
);
$output = array(
'status' => 'ok',
'content' => $grades
);
echo json_encode($output);
So applying the rules I have outlined above will result in the JSON below.
{"status":"ok","content":[{"name":"tom","grade":"A"},{"name":"jeff","grade":"B"},{"name":"lise","grade":"C"}]}
I personally think you should use this since it is a better representation than the one you desire to have. However, the codes below should give you your results.
$grades = array(
'tom' => 'A',
'jeff' => 'B',
'lisa' => 'B',
);
$output = array(
'status' => 'ok',
'content' => $grades
);
echo json_encode($output);
Output:
{"status":"ok","content":{"tom":"A","jeff":"B","lisa":"B"}}
The reason you are getting the 'extra' \ is that what you say you want as output is not valid JSON. PHP's default function has it right, yours will not parse in a JSON parser.
If you really want almost-json, you'll have to roll your own.
you have to change the format of $grade
array to get the desired output . try below
$grade_format= array();
foreach($grades as $k=>$v)
{
//array_push($output,$k);
$grade_format[$v['name']]=$v['grade'];
}
//echo "<pre>";
//print_r($grade_format);
$output=array
( 'status'=>'ok',
'content'=>$grade_format
);
echo "<pre>";
echo json_encode($output,JSON_FORCE_OBJECT);
//output would be
{"status":"ok","content":{"tom":"A","jeff":"B","lisa":"C"}}
is that you want??
REFERENCE
精彩评论