php - insert an array as an object property (json) -


i expect result

[     {         "uid": "1",         "firstname": "james",         "lastname": "bond",         "task": [             {                 "task": "teaching"             },             {                 "task": "tutoring"             }         ]     } ] 

where result1 , result2 following: $result1

[{"uid":"1","firstname":"james","lastname":"bond"}] 

$result2

[{"task":"teaching"},{"task":"tutoring"}] 

i tried

$result1[0]['tabs'] = $result2; echo json_encode($result1); 

but says fatal error: cannot use object of type stdclass array

you have this..

$result1='[{"uid":"1","firstname":"james","lastname":"bond"}]'; $result2='[{"task":"teaching"},{"task":"tutoring"}]'; $arr1 = json_decode($result1,true); $arr2 = json_decode($result2,true); $arr1[0]['task'] = $arr2; $finaljson = json_encode($arr1); echo $finaljson; 

output :

   [{"uid":"1","firstname":"james","lastname":"bond","task":[{"task":"teaching"},{"task":"tutoring"}]}] 

demonstration


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -