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

  • SEARCH
  • Home
  • 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 8017981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:00:00+00:00 2026-06-04T21:00:00+00:00

I am trying to make json data in php using json_encode() function. Then I

  • 0

I am trying to make json data in php using json_encode() function. Then I will use this data on the client side. I acheived some of the part. My current code displays json data in this format

{
"data":
{
    "tag":"home",
    "success":1,
    "error":0,
    "uid":"4fc8f94f1a51c5.32653037",
    "name":"Zafar Saleem",
    "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile1.jpg",
    "places":
    {
        "place_photo":"http:\/\/example.info\/android\/places_photos\/place1.jpg",
        "created_at":"2012-06-02 00:00:00",
        "seeked":"0"
    }
}
}
{
"data":
{
    "tag":"home",
    "success":1,
    "error":0,
    "uid":"4fc9c413554104.22444656",
    "name":"Name",
    "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile2.jpg",
    "places":
    {
        "place_photo":"http:\/\/example.info\/android\/places_photos\/place2.jpg",
        "created_at":"2012-06-03 00:00:00",
        "seeked":"0"
    }
}
}
{
"data":
{
    "tag":"home",
    "success":1,
    "error":0,
    "uid":"4fc9c48c529675.45551665",
    "name":"Name",
    "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile3.jpg",
    "places":
    {
        "place_photo":"http:\/\/example.info\/android\/places_photos\/place3.jpg",
        "created_at":"2012-06-04 00:00:00",
        "seeked":"20"
    }
}
}

what I want to show above data in this form

{
"data": 
[
    {
        "tag":"home",
        "success":1,
        "error":0,
        "uid":"4fc8f94f1a51c5.32653037",
        "name":"Zafar Saleem",
        "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile1.jpg",
        "places":
        {
            "place_photo":"http:\/\/example.info\/android\/places_photos\/place1.jpg",
            "created_at":"2012-06-02 00:00:00",
            "seeked":"0"
        }
    },
    {
        "tag":"home",
        "success":1,
        "error":0,
        "uid":"4fc9c413554104.22444656",
        "name":"Name",
        "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile2.jpg",
        "places":
        {
            "place_photo":"http:\/\/example.info\/android\/places_photos\/place2.jpg",
            "created_at":"2012-06-03 00:00:00",
            "seeked":"0"
        }
    },
    {
        "tag":"home",
        "success":1,
        "error":0,
        "uid":"4fc9c48c529675.45551665",
        "name":"Name",
        "profile_photo":"http:\/\/example.info\/android\/profile_photos\/profile3.jpg",
        "places":
        {
            "place_photo":"http:\/\/example.info\/android\/places_photos\/place3.jpg",
            "created_at":"2012-06-04 00:00:00",
            "seeked":"20"
        }
    }
]
}

here is my php code that generates json data

database function that gets data from database

 public function getHome() {
    $result = mysql_query("SELECT * FROM places") or die(mysql_error());
    // check for result
    $no_of_rows = mysql_num_rows($result);
    if ($no_of_rows > 0) {
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $data[] = $row;
        }
        return $data;
        /*
        $result = mysql_fetch_array($result);
        return $result;
        */
    } else {
        // user not found
        return false;
    }
}

here is where I make json on php

if($db->getHome()) {
        $data = $db->getHome();
        foreach($data as $r) {
            $response['success'] = 1;
            $response['uid'] = $r['uid'];
            $response['name'] = $r['name'];
            $response['profile_photo'] = $r['profile_photo_path'];
            $response['places']['place_photo'] = $r['place_photo_path'];
            $response['places']['latitude'] = $r['latitude'];
            $response['places']['longitude'] = $r['longitude'];
            $response['places']['created_at'] = $r['created_at'];
            $response['places']['seeked'] = $r['total_completed'];
            echo json_encode(array('data' => $response));
        }
    } else {
        $response['error'] = 1;
        $response['error_msg'] = 'No data available';
        echo json_encode($response);
    }
  • 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-04T21:00:02+00:00Added an answer on June 4, 2026 at 9:00 pm

    Consider making the code look like this:

    // this function turns a database row into data for frontend use
    function convert_to_response($r)
    {
      return array(
        'success' => 1,
        'uid' => $r['uid'],
        'name' => $r['name'],
        'profile_photo' => $r['profile_photo_path'],
        'places' => array(
          'place_photo' => $r['place_photo_path'],
          'latitude' => $r['latitude'],
          'longitude' => $r['longitude'],
          'created_at' => $r['created_at'],
          'seeked' => $r['total_completed'],
        ),
      );
    }
    
    if($db->getHome()) {
            $data = $db->getHome();
            echo json_encode(array(
                'data' => array_map('convert_to_response', $data) // convert data array
            ));
        } else {
            $response['error'] = 1;
            $response['error_msg'] = 'No data available';
            echo json_encode($response);
        }
    

    Your code was echoing out each data row by itself, so that’s why you weren’t seeing an array (using square brackets).

    I’ve also made your code a bit clearer by moving the response conversion into a separate function; this way it can be used by functions such as array_map to convert an array of data in one format to another format.

    Hope it helps.

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

Sidebar

Related Questions

I'm trying to make a datatable using YUI with JSON returned data. Included is
I trying to make my first AJAX with JSON call using jQuery and CodeIgniter.
I'm trying to make an API for my rails application using JSON responses to
Trying to make this function dynamic instead of static. This was the original code
Trying to make a simple client that consumes a web service using RemObjects SDK,
I'm (trying to) using JSON with PHP. I want to plot a graph using
I'm experimenting/learning with java and json. I'm trying to make my own data for
I'm trying to make a very simple query: $.ajax({ url: ajaxfunc.php, dataType: 'json', success:
I have a PHP file using json_encode to make a single call, but it
I'm trying to read some JSON data from the Tumblr API. I'm using the

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.