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"}]}]
Comments
Post a Comment