Nested Array to JSON in Php MySQL

  • Status Closed
  • Budget N/A
  • Total Bids 2

Project Description

I have the following data in mysql table


1 | Parent Msg | 10/10/2012| null

2 |Reponse to parent Msg| 10/10/2012| 1

3 |Response to parent msg | 10/10/2012|1

I need the nested output(multidimensional array) in JSON like this:

[Messages:[Msgid, MsgBody,Msgdate, Replies[ MsgId,MsgBody,Msgdate]]

This will output all the messages and also the replies for each message as an array.


require_once("[url removed, login to view]");

$response = array();

$messages = array();

$query="SELECT * FROM (SELECT username, userpicurl, userlocation, userbio, msgbody, msgsenddate, msgpersonal, msgparentid, msgdateposted FROM tblMsg INNER JOIN tblUser ON [url removed, login to view] = [url removed, login to view] ORDER BY MsgId DESC LIMIT 0 , 10) AS tbl ORDER BY [url removed, login to view]";

$result = mysql_query($query,$con) or die('Errant query: '.$query);



$username =$row['username'];

$picurl =$row['userpicurl'];


$messages[] = array('username'=> $username,'picurl'=>$picurl,'location'=>$location,'bio'=>$bio, 'msgbody'=> $msgbody,'senddate'=>$senddate,'personal'=>$personal,'receiverid'=>$receiverid); }

$response['messages'] = $messages;



Get free quotes for a project like this

Looking to make some money?

  • Set your budget and the timeframe
  • Outline your proposal
  • Get paid for your work

Hire Freelancers who also bid on this project

    • Forbes
    • The New York Times
    • Time
    • Wall Street Journal
    • Times Online