Json formats

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adityagis
Forum Newbie
Posts: 1
Joined: Tue Sep 08, 2015 10:33 am

Json formats

Post by adityagis »

json1.png
json1.png (10.54 KiB) Viewed 746 times
Hi Team,
I need to create a JSON from my php. As of now I am able to get the JSON, but it is needed in a different format.
Mentioned below is the code I am using

<?php
$connectString = 'host=localhost dbname=postgis_21_sample user=postgres password=postgresql';
$link = pg_connect($connectString);

if (!$link) {
echo "error";
} else {

$result = pg_query($link, $sql);

//$rows = array();
while($r = pg_fetch_assoc($result)) {
$rows[] = $r;
}

print json_encode($rows,true);
}
?>

json.png
Attached is the image json.png which show how the following code shows the output.

json1.png
json1.png (10.54 KiB) Viewed 746 times
Attached is the image of the required format.

Please help me to get the desired format.


Thanks
Aditya Kumar
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Json formats

Post by Christopher »

Try the JSON_FORCE_OBJECT flag.
(#10850)
Post Reply