Hi I want JSON response in below format using PHP.
{
"response": {
"status": "ok",
"results": [
{
"id": "123",
"fields": {
"trailText": "SomeText",
"thumbnail": "URL"
},
"tags": [
{
"id": "profile/amy-walker",
"type": "contributor",
]
}
]
}
}
My PHP Code
while($row = mysqli_fetch_assoc($result1))
{
$temp_array[$num_rows]['id']= $row["id"];
//Some more code
}
echo $post = json_encode(array('response'=>$myObj,$temp_array));
But I got response like this
{
"response": {
"status": "ok"
},
"results": [
{
"id": "123",
"fields": {
"trailText": "SomeText",
"thumbnail": "URL"
},
"tags": [
{
"id": "profile/amy-walker",
"type": "contributor",
]
}
]
}
I want my array should be bound inside response.
$myObj is
object(stdClass)#4 (8) {
["status"]=> string(2) "ok"
["userTier"]=> string(9) "developer"
["total"]=> int(8282)
["startIndex"]=> int(1)
["pageSize"]=> int(12)
["currentPage"]=> int(1)
["pages"]=> int(8282)
["orderBy"]=> string(6) "newest"
}
Death-is-the-real-truth
72.3k10 gold badges59 silver badges106 bronze badges
1 Answer 1
Based on your comment regarding $myObj data. It seems @Nigel Ren code will work for you with a small modification.
do like this:
$result = array($myObj->status,$temp_array);
echo $post = json_encode(array('response'=>$result));
answered Apr 6, 2020 at 7:42
Death-is-the-real-truth
72.3k10 gold badges59 silver badges106 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-php
json_encode(array('response'=>array($myObj,$temp_array)))to group together the last bits of data (i.e. add an extraarray()in)$myObjby doingvar_dump($myObj);json_encodeproduces a different output, then your input data is in the wrong format