M. Schaetzl
M. Schaetzl

Reputation: 251

Merge nested arrays in PHP returning json

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!

Upvotes: 0

Views: 46

Answers (2)

Amit Visodiya
Amit Visodiya

Reputation: 813

Try this

/* Get most frequently used applications */
$apps = array();
$sql = "SELECT * FROM MostFrequentlyApps WHERE UID = 'yxz'";
$result = mysqli_query($con, $sql);

while($obj = mysqli_fetch_object($result)) {
    $apps[] = $obj;
}



/* Get user favorites */
$favs = array();
$sql = "SELECT * FROM UserFavorites WHERE UID = 'yxz'";
$result = mysqli_query($con, $sql);

while($obj = mysqli_fetch_object($result)) {
    $favs[] = $obj;
}

$allData = array("apps"=> $apps,"favs"=> $favs);

echo json_encode($allData);

Upvotes: 0

u_mulder
u_mulder

Reputation: 54831

Here what you need to do:

// result array with two sub-arrays
$allData = array(
    'favs' => array(),
    'apps' => array(),
);

/* Get most frequently used applications */
$sql = "SELECT * FROM MostFrequentlyApps WHERE UID = 'yxz'";
$result = mysqli_query($con, $sql);

// add apps right to `apps` sub=array
while($obj = mysqli_fetch_object($result)) {
    $allData['apps'][] = $obj;
}

/* Get user favorites */
$sql = "SELECT * FROM UserFavorites WHERE UID = 'yxz'";
$result = mysqli_query($con, $sql);

// add favourites right to `favs` sub=array
while($obj = mysqli_fetch_object($result)) {
    $allData['favs'][] = $obj;
}

echo json_encode($allData);

Upvotes: 1

Related Questions