我想在结果中添加一些条目
$rows = $sth->fetchAll(PDO::FETCH_ASSOC);
但是,我无法在同一数组索引中添加额外的键/值对。
$rows 的转储给出了这个:
array(1) {
[0]=>
array(9) {
["first_name"]=>
string(4) "Gary"
["last_name"]=>
string(6) "Cotton"
["job_title"]=>
string(11) "Web Manager"
["telephoneNumber"]=>
string(12) "123-456-7890"
["email"]=>
string(22) "first.last@org.ca"
["street"]=>
string(19) "street"
["city"]=>
string(6) "city"
["prov"]=>
string(2) "ON"
["postal"]=>
string(7) "K1A 0X1"
}
}
我要补充
$response = array('max_upo' => "false", 'num_contacts' => "1", 'org_acronym' => "TEST");
这样转储看起来像
array(1) {
[0]=>
array(12) {
["first_name"]=>
string(4) "Gary"
["last_name"]=>
string(6) "Cotton"
["job_title"]=>
string(11) "Web Manager"
["telephoneNumber"]=>
string(12) "123-456-7890"
["email"]=>
string(22) "first.last@org.ca"
["street"]=>
string(19) "street"
["city"]=>
string(6) "city"
["prov"]=>
string(2) "ON"
["postal"]=>
string(7) "K1A 0X1"
["max_upo"]=>
string(5) "false"
["num_contacts"]=>
string(1) "1"
["org_acronym"]=>
string(4) "TEST"
}
}
我试过了
$rows[0][] = $response;
$rows = array_merge($response, $rows);
和变化无济于事。响应数组被添加,但是在一个新的索引中。
最佳答案
一个基本的方法是:
foreach ($response as $k=>$v) {
$rows[0][$k] = $v;
}
print_r($rows[0]);
关于php - 如何将 View 值添加到 PDO fetchAll 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31970220/