I have `PostgreSQL` function (tree structure),i need to convert the output to `json` tree format.

my `PostgreSQL` function 

```
RETURN QUERY
WITH RECURSIVE manger_hierarchy(e_id,e_name,m_name,m_id) AS 
(
    SELECT 
        e.emp_id,e.emp_name,
        m.emp_name AS man_name,e.emp_man_id 
    FROM emp_det e 
    LEFT JOIN emp_det m ON e.emp_man_id=m.emp_id
    WHERE e.emp_id = id
    UNION
    SELECT 
        rp.emp_id,rp.emp_name,
        mh.e_name AS manager,rp.emp_man_id
    FROM manger_hierarchy mh
    INNER JOIN emp_det rp ON mh.e_id = rp.emp_man_id
)
SELECT h.e_id,h.e_name,h.m_name,h.m_id
FROM manger_hierarchy h;

```

and my `php` scrip is 

`header('Content-Type: application/json');`
`$dbconn=pg_connect('host=localhost port=5432 dbname=test1 user=someuser password=somepass');`
`$x=$_GET['id'];`
`$result=pg_query($dbconn,"SELECT * FROM employe_manager_details($x)");`
`$y=pg_fetch_all($result);`
`echo json_encode($y, JSON_PRETTY_PRINT);`
`pg_close($dbconn);`

my current result 
`[
 {
 "e_id": "4",
 "e_name": "john",
 "e_manager": "steve",
 "e_man_id": "2"
 },
 {
 "e_id": "8",
 "e_name": "jake",
 "e_manager": "john",
 "e_man_id": "4"
 },
 {
 "e_id": "9",
 "e_name": "wilson",
 "e_manager": "john",
 "e_man_id": "4"
 }
]`
expect result 

`[
 {
 "e_id": "4",
 "e_name": "john",
 "e_manager": "steve",
 "e_man_id": "2"
 },
 {
 "e_id": "8",
 "e_name": "jake",
 "e_manager": "john",
 "e_man_id": "4"
 },
 {
 "e_id": "9",
 "e_name": "wilson",
 "e_manager": "john",
 "e_man_id": "4"
 }
]`