help needed in building json data

3xtr3m3d
3xtr3m3d used Ask the Experts™
on
Hi

any idea how to build this structure with json using php

"data" : [
				{ 
					"data" : "My Documents", 
					"children" : [ {"attr":{"id":"2","rel":"file"},"data":"file1.doc","state":""} ]
				}
			]

Open in new window


Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Expert of the Year 2008
Top Expert 2008

Commented:
Use json_encode:
http://us3.php.net/manual/en/function.json-encode.php

Refer to the example sections on that page


//Something like this:

$o = array( "data" => array(
  "data" => "My Documents",
  "children" => array(
    "attr" => array(
      "id" => 2,
      "rel" => "file"
      ),
      "data" => "file1.doc",
      "state" => ""
    )
  )
);

Expert of the Year 2008
Top Expert 2008
Commented:
try:
$obj = array( "data" => array(
							array(
								"data" => "My Documents",
								"children" => array(
													array(
															"attr" => array(
																			"id" => 2,
																			"rel" => "file"
																		),
															"data" => "file1.doc",
															"state" => ""
														)
											)
								)
						)
		);
		
echo json_encode($obj);

Open in new window

Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Hi

Thanks all for the replies guys.

hielo: thanks i changed your solution like this and it works

$obj = array(
				array(
								"data" => "My Documents",
								"children" => array(
													array(
															"attr" => array(
																			"id" => 2,
																			"rel" => "file"
																		),
															"data" => "file1.doc",
															"state" => ""
														)
											)
								)
						
		);
		
echo json_encode($obj);

Open in new window


any idea how to build this with two mysql queries? like getting
"data" => "My Documents"  from 1 query and contents for children with second query.

Thanks
Expert of the Year 2008
Top Expert 2008

Commented:
It sounds like you will need nested while loops.  Open a new thread and post your php code related to these queries.

Author

Commented:
Thanks posted new question on

New Question

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial