I'm trying to merge at least two arrays into JSON using PHP. Currently that's my code:
/* Get most frequently used applications */
$var = array();
$sql = "SELECT * FROM MostFrequentlyApps WHERE UID = 'yxz'";
$result = mysqli_query($con, $sql);
while($obj = mysqli_fetch_object($result)) {
$var[] = $obj;
}
$allData = array_merge($allData,$var);
/* Get user favorites */
$var = array();
$sql = "SELECT * FROM UserFavorites WHERE UID = 'yxz'";
$result = mysqli_query($con, $sql);
while($obj = mysqli_fetch_object($result)) {
$var[] = $obj;
}
$allData = array_merge($allData,$var);
echo json_encode($allData);
And the JSON-Code I get looks like this:
[
{
"UID": "xyz",
"Application": "Test",
"AppLink": "http://www.google.com",
"AppIcon": "icon.png"
},
{
"UID": "xyz",
"Application": "Test2",
"AppLink": "http://www.facebook.com",
"AppIcon": "icon2.png"
},
{
"UID": "xyz",
"URL": "www.yahoo.com"
},
{
"UID": "xyz",
"URL": "www.bing.com"
}
]
But I would need the results look like this, so kind of a hierarchy with one part sowing the apps and the other part showing the favorites:
{
"apps":
[
{
"UID": "xyz",
"Application": "Test",
"AppLink": "http://www.google.com",
"AppIcon": "Icon.png"
},
{
"UID": "xyz",
"Application": "Test2",
"AppLink": "http://www.facebook.com",
"AppIcon": "icon2.png"
}
]
"favs":
[
{
"UID": "xyz",
"URL": "www.yahoo.com"
},
{
"UID": "xyz",
"URL": "www.bing.com"
}
]
}
But my problem is, that I don't know how to merge those two arrays and getting this hierarchic structure in the end. Do I have to merge the arrays first and then encode those to JSON? Maybe someone can give me a hint.
Thanks in advance!