0

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!

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.