如何将视图值添加到PDO fetchAll结果

时间:2015-08-12 16:00:22

标签: php

我想在

的结果中添加一些条目
$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);

和变化无济于事。响应数组被添加,但是在新索引中。

1 个答案:

答案 0 :(得分:1)

基本方法是:

foreach ($response as $k=>$v) {
    $rows[0][$k] = $v;
}
print_r($rows[0]);