I have a json that is look like this
{
"data": [
data here
]
}
and here is how i fill it up
$json = array();
foreach ($result as $key => $value) {
$json['data'][] = $value;
}
and here is how i display it
echo json_encode($json,JSON_PRETTY_PRINT);
what if the data for is empty? how can I display an empty json because my php only display []
when it is empty
like maybe something like this
{
"data": [
]
}
答案 0 :(得分:0)
I would suggest explicitly defining your structure before adding records to your 'data' index. Currently, the 'data' property is only created during result iteration, which is why you're seeing a string of []
wen there are no records.
$json = array( 'data' => array() );
This will also help to avoid any warnings/notices generated by PHP.