Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9318449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:07:51+00:00 2026-06-19T03:07:51+00:00

Hi i have written an android app which interacts with php to get data

  • 0

Hi i have written an android app which interacts with php to get data from the database.

I need the json to return the data back in this format.

[
    {
        "outlet_id"    :"1",
        "outlet_desc"  :"BigBazaar",
        "outlet_loc"   :"Jayanagar4thBlock",
        "outlet_image" :"http%3A%2F%2Flocalhost%2FFlutura%2FPHP%2FAssets%2Fimages%2FBigBazaar.png",
        "recommended_products": [
                                    {
                                        "item_id":"3",
                                        "item_desc":"Dettol",
                                        "item_image":"http%3A%2F%2Flocalhost%2FFlutura%2FPHP%2FAssets%2Fimages%2FDettol.png"
                                    }
                                ]
    },
    {
        "outlet_id":"2",
        "outlet_desc":"FoodWorld",
        "outlet_loc":"Basavanagudi",
        "outlet_image":"http%3A%2F%2Flocalhost%2FFlutura%2FPHP%2FAssets%2Fimages%2FFoodWorld.png","
        recommended_products":[
                                {
                                    "item_id":"3",
                                    "item_desc":"Dettol",
                                    "item_image":"http%3A%2F%2Flocalhost%2FFlutura%2FPHP%2FAssets%2Fimages%2FDettol.png"
                                },
                                {
                                    "item_id":"3",
                                    "item_desc":"Colgate",
                                    "item_image":"http%3A%2F%2Flocalhost%2FFlutura%2FPHP%2FAssets%2Fimages%2FColgate.png"
                                }
                              ]
    },
    {
        "outlet_id":"5",
        "outlet_desc":"Total",
        "outlet_loc":"MurgeshPalaya",
        "outlet_image":"http%3A%2F%2Flocalhost%2FFlutura%2FPHP%2FAssets%2Fimages%2FTotal.png",
        "recommended_products":[
                                {
                                    "item_id":"3",
                                    "item_desc":"Dettol",
                                    "item_image":"http%3A%2F%2Flocalhost%2FFlutura%2FPHP%2FAssets%2Fimages%2FDettol.png"


                                },
                                {
                                    "item_id":"3",
                                    "item_desc":"Colgate",
                                    "item_image":"http%3A%2F%2Flocalhost%2FFlutura%2FPHP%2FAssets%2Fimages%2FColgate.png"

                                }
                               ]
     }
]

But right now it is giving in the following format.

[
    [
        {
            "outlet_id": "1",
            "outlet_name": "Big Bazaar",
            "lat": "12.9285690",
            "lng": "77.5831100",
            "outlet_image": "images/BigBazaar.png",
            "outlet_location": "Jayanagar 4th Block"
        }
    ],
    [
        {
            "outlet_id": "2",
            "outlet_name": "Food World",
            "lat": "12.9392350",
            "lng": "77.5780680",
            "outlet_image": "images/FoodWorld.png",
            "outlet_location": "Basavanagudi"
        }
    ],
    [
        {
            "outlet_id": "5",
            "outlet_name": "Total",
            "lat": "12.9589340",
            "lng": "77.6571610",
            "outlet_image": "images/Total.png",
            "outlet_location": "Murugeshpalya"
        }
    ],
    [
        {
            "outlet_id": "6",
            "outlet_name": "Big Bazaar",
            "lat": "12.9324963",
            "lng": "77.6228463",
            "outlet_image": "images/BigBazaar.png",
            "outlet_location": "Koramangala"
        }
    ],
    [
        {
            "outlet_id": "7",
            "outlet_name": "Food World",
            "lat": "12.9785055",
            "lng": "77.6379353",
            "outlet_image": "images/FoodWorld.png",
            "outlet_location": "Indira Nagar"
        }
    ]
]

This is my php code.

<?php
error_reporting(0);

//$url = $_GET['url'];
//$mR = $_GET['mRequest'];
//$mOid = $_GET['mOutletId'];
//$mloc = $_GET['mLocation'];
//connect to the db
$user = "root";
$pswd = "";
$db = "recommendations_db";
$host = "localhost";
$conn = mysql_connect($host, $user, $pswd);
mysql_select_db($db);
//if($mR == 'outlets' && $mloc = 'all'){
$query = "SELECT * FROM outlets";
$result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());

while($row = mysql_fetch_assoc($result))
{
  $output[] = array($row); 
}
print( json_encode($output) );
?>

How can i modify the above php code to return in the 1st format. Please help as i am not so good in php.

Table Structure.

Outlets Table.

It contains the following fields.

outlet_id outlet_name lat lng outlet_image outlet_location

Recommendations table.

It contains the following fields.

item_id outlet_id

Items table.

It contains the following fields.

item_id item_name item_image item_price category_id

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T03:07:52+00:00Added an answer on June 19, 2026 at 3:07 am

    Ok so it’s the missing sub-array. You want to create a JSON string, not a object, from a multidimensional associative array.

    There are faster ways to select these rows but this is easy to follow.

    $query = "SELECT * FROM outlets";
    $result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());
    
    $output=array(); # don't leave it undefined
    while($row = mysql_fetch_assoc($result))
    {
        # guessing this query
        $query2 = "SELECT * FROM recommended_products WHERE outlet_id=".$result['outlet_id'];
    
        # now we create the sub array recommended_products
        $row['recommended_products']=array();
        $result2 = mysql_query($query2) or die("Unable to verify user because : " . mysql_error());
        while($row2 = mysql_fetch_assoc($result2)){
            $row['recommended_products'][]=$row2;
        }
    
        # then add the modified row to the output
        $output[] = $row; 
    }
    print( json_encode($output) );
    

    Also, mysql_ is depreciated. Probably no genuine need to rush but it’s worth at least looking at converting to mysqli or PDO.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In an android app, I have written code to get geo data through GPS
I have an android app, written in java which uses AsyncTask to download data.
I have written an app which fetches data from a server. I am not
I have written an android app which listens to the phone signal strength using
I have written a simple database driven app in C# which uses a 2
I have written an app which streaming video using HLS streaming to android 4
Hi i have written an android app which uses mysql db my problem is
I have written an Android application that uses SQLite database which is saved in
I've written an Android app which various abstract classes that perform useful functions. These
I have written an android app that saves (potentially) large files to the SD

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.