I am getting dataset from a database query as object. What I want to do is to manipulate current output and create an additional array to store some of the output to have it compatible for my json input in another call.
Current json_encode($myObj)
output
[
{
"entry": "10",
"displayid": "100",
"folder": "1"
},
{
"entry": "20",
"displayid": "200",
"folder": "2"
},
{
"entry": "30",
"displayid": "300",
"folder": "3"
}
]
What I am trying to have through this data is;
[
{
"file": {
"entry": 10,
"displayid": 100
},
"folder": 1
},
{
"file": {
"entry": 20,
"displayid": 200
},
"folder": 2
},
{
"file": {
"entry": 30,
"displayid": 300
},
"folder": 3
}
]
I tried the following, but it gives me completely weird output;
foreach ($result as $res) {
$ho = new stdClass();
$ho->file = [$ho->entry = $res->entry, $ho->displayid = $res->displayid];
$ho->folder = $res->folder;
}
CodePudding user response:
foreach ($result as $res) {
$ho = new stdClass();
$ho->file = (object)['entry' => $res->entry, 'displayid' => $res->displayid];
$ho->folder = $res->folder;
$newObj[] = $ho;
}
print_r($newObj);