0

I have the following JSON array:

[
  {"r1t7pjT4wn":{"Title":"test","Meta":"test","SM_D":"test","BIG_D":"test"}},
  {"3rMlBu6LpZ":{"Title":"test1","Meta":"test1","SM_D":"test1","BIG_D":"test1"}}
]

When I do json_decode, I expect to see:

Array ( 
  "r1t7pjT4wn" => Array ( [Title] => test [Meta] => test [SM_D] => test [BIG_D] => test ),
  "3rMlBu6LpZ" => Array ( [Title] => test1 [Meta] => test1 [SM_D] => test1 [BIG_D] => test1 )
)

However, PHP yields:

Array ( 
  [0] => Array ( [r1t7pjT4wn] => Array ( [Title] => test [Meta] => test [SM_D] => test [BIG_D] => test ) ) 
  [1] => Array ( [3rMlBu6LpZ] => Array ( [Title] => test1 [Meta] => test1 [SM_D] => test1 [BIG_D] => test1 ) ) 
)
0

2 Answers 2

3

if you can't change source json, add one function call

call_user_func_array('array_merge', json_decode($json, true));
Sign up to request clarification or add additional context in comments.

Comments

2

This is because your data is an array! If it were an associative array (i.e. if it began with { and ended with }) then json_decode would have the output you expect.


Change your JSON to:

{
   "r1t7pjT4wn":{"Title":"test","Meta":"test","SM_D":"test","BIG_D":"test"},
   "3rMlBu6LpZ":{"Title":"test1","Meta":"test1","SM_D":"test1","BIG_D":"test1"}
}

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.