0

我正在循环中生成一个结果数组,我想添加到循环中的数组中。显然数组推送将不起作用,因为它们是键值对。

foreach($res as $ap){
    $this->db->where('event_time >', $ev_time);
    $this->db->where('event_ID', $ap['event_id']);
    $query = $this->db->get('events');
    if($query->num_rows() > 0){
        $result = $query->result_array();
        $linked[] = $result[0]; // <-- want to add key, value pair within this
        $linked['new_key'] = $new_value; // <-- did not work :(
    }
}

我怎样才能做到这一点?

此数组稍后也会与另一个数组合并。如果我向它添加一个不在另一个数组中的额外键、值对,会破坏合并吗?

4

3 回答 3

0

也许尝试类似:

$myarray[] = array($key, $value);
于 2013-10-10T11:27:20.537 回答
0

尝试这个

$i = 0;
foreach($res as $ap){
    $this->db->where('event_time >', $ev_time);
    $this->db->where('event_ID', $ap['event_id']);
    $query = $this->db->get('events');
    if($query->num_rows() > 0){
        $result = $query->result_array();
        $linked[$i] = $result[0]; // <-- want to add key, value pair within this
        $linked[$i]['new_key'] = $new_value;
        $i++;
    }
}

$i将使您能够将新值添加到与$result[0]写入相同的元素中。

于 2013-10-10T11:19:32.793 回答
0
foreach($res as $ap){
    $this->db->where('event_time >', $ev_time);
    $this->db->where('event_ID', $ap['event_id']);
    $query = $this->db->get('events');
    if($query->num_rows() > 0){
        $result = $query->result_array();
        $linked[] = $result[0]; // <-- want to add key, value pair within this

        $linked['new_key'][] = $new_value;
    }
}
于 2013-10-10T11:13:16.857 回答