我想要实现以下数组格式:
我现在有一个数组的第一部分和一个foreach循环:
只有上面输出:
{
"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": [ ... ]
中。
json_encode
,但是你的问题并没有展示出来。 - Steve