【发布时间】:2017-09-16 19:02:06
【问题描述】:
我想实现以下数组格式:
{
"success": true,
"results": [
{
"name" : "Choice 1",
"value" : "value1",
"text" : "Choice 1"
},
{
"name" : "Choice 2",
"value" : "value2",
"text" : "Choice 2"
}
]
}
但是,我使用 PHP 和 foreach 循环从我的数据库中返回一些值:
//Search for clients in our database.
$stmt = $dbh->prepare("SELECT * FROM customers");
$stmt->execute();
$showAll = $stmt->fetchAll();
然后我得到了数组的第一部分,以及我的 foreach 循环:
$data = array(
"success" => false,
"results" => array()
);
foreach ($showAll as $client) {
$data_array[] =
array(
'name' => $client['name'],
'value' => $client['name'],
'text' => $client['name']
);
}
以上仅输出:
[
{
"name":"Choice 1",
"value":"value 1",
"text":"Choice 1"
},
{
"name":"Choice 2",
"value":"value2",
"text":"Choice 2"
}
]
所以它缺少我原始数组的顶部 - 但我想遍历每个数据库结果 "results": [ ... ]
【问题讨论】:
-
"Above only outputs:"请出示您的输出代码 -
@Steve 它显示在文本“以上仅输出:”的下方
-
这个问题中没有代码可以输出任何东西。我猜你在某个地方打电话给
json_encode,但你的问题并没有表明