2020. 10. 14. 15:27ㆍPHP
no square bracket json array
Asked 7 years, 6 months ago
Active 9 months ago
Viewed 39k times
30
11
When trying to access an API the JSON array must be parsed like this
{"item":[{"id":"123456", "name":"adam"}]}
But when i'm doing the following code
$data = array("item" => array("id" => "123456", "name" => "adam")); echo json_encode($data);
it returns the json array without squared brackets as follows
{"item":{"id":"123456","name":"adam"}}
I've spent hours trying to figure out how to fix this and just can't think of a solution
share improve this question follow
asked Mar 21 '13 at 22:55
3,3844 gold badges23 silver badges30 bronze badges
4 Answers
63
You need to wrap things in another array:
$data = array("item" => array(array("id" => "123456", "name" => "adam")));
This will be more understandable if we use the equivalent PHP 5.4 array syntax:
$data = [ "item" => [ ["id" => "123456", "name" => "adam"] ] ];
Compare this with the JSON:
{ "item": [ {"id":"123456", "name":"adam" } ] }
The only thing to explain is why one of the PHP arrays remains an array [] in JSON while the other two get converted to an object {}. But the documentation already does so:
When encoding an array, if the keys are not a continuous numeric sequence starting from 0, all keys are encoded as strings, and specified explicitly for each key-value pair.
share improve this answer follow
answered Mar 21 '13 at 22:57
384k68 gold badges675 silver badges758 bronze badges
-
When using that code as provided it's placing the squared brackets outside everything instead of inside item ? – Curtis Crewe Mar 21 '13 at 23:01
-
@CurtisCrewe: Sorry, typo. Fixed now. – Jon Mar 21 '13 at 23:01
-
1
Just what I was looking for – JoshHighland Aug 19 '15 at 7:32
7
Before reading this post, I had this:
echo json_encode($data);
After reading this post:
echo json_encode(array($data));
Brackets appeared on the start and end of the JSON object.
:)
share improve this answer follow
answered Mar 5 '16 at 4:57
5418 silver badges21 bronze badges
-
Don't know why the downvote because it just worked for me (+1). – Jose Manuel Abarca Rodríguez Jul 11 '16 at 20:41
-
Useful, but if you want the brackets IN the data arrays, just use something like this: $MyArray = array( "something" => [array("foo" => "bar")] ); – Jimmy Adaro Feb 7 '18 at 22:31
3
It become handy when using this way, so you can add more items on the array
$val = array(); $val["id"]="123456"; $val["name"]="adam"; $data = array(); $data["item"][]=$val; echo json_encode($data);
And it will ouput below:
{"item":[{"id":"123456", "name":"adam"}]}
share improve this answer follow
answered Mar 22 '17 at 8:52
811 silver badge5 bronze badges
0
Based on your code:
$data = array("item" => array("id" => "123456", "name" => "adam")); echo json_encode($data);
You should just use another array() to force the bracket.
$data = array("item" => array(array("id" => "123456", "name" => "adam"))); echo json_encode($data);
This will return the value you are looking for:
{ "item": [ {"id":"123456", "name":"adam" } ] }
share improve this answer follow
answered Jan 14 at 18:51
1
'PHP' 카테고리의 다른 글
vscode xdebug 설정 spawn php ENOENT 에러 해결 (0) | 2021.01.28 |
---|---|
xampp, xdebug, vscode 세팅 (0) | 2021.01.27 |
PHP sessino_write_close error (0) | 2020.09.10 |
php Maximum execution time of 30 seconds exceeded (0) | 2020.08.25 |
php strtotime, date, mktime, datetime, timestamp (0) | 2020.08.20 |