T
T
Triglav772019-08-02 14:31:19
PHP
Triglav77, 2019-08-02 14:31:19

How to convert json to a specific form?

I collect an array in a loop from the database

$data[] = [
    'proto' => $protos[$protoid],
    'localip' => long2ip($row['nIPv4Left']),
    'port' => $row['nPortLeft']
];

converting to json
echo json_encode($data);
I get this look
[
  {"proto":null,"localip":"0.0.0.222","port":11018},
  {"proto":null,"localip":"192.168.69.138","port":41752},
  {"proto":null,"localip":"0.0.0.76","port":46907},
  {"proto":null,"localip":"0.0.0.244","port":26114},
  {"proto":null,"localip":"0.0.0.242","port":1112}
]

how to convert it to this json
{  
    "data":
  [
    {"proto":null,"localip":"0.0.0.222","port":11018},
    {"proto":null,"localip":"192.168.69.138","port":41752},
    {"proto":null,"localip":"0.0.0.76","port":46907},
    {"proto":null,"localip":"0.0.0.244","port":26114},
    {"proto":null,"localip":"0.0.0.242","port":1112}
  ]
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question