-1

我正在尝试将新数据推送到现有数组中$query

$query = $this->db->get('questions');
foreach ($query->result_array() as $row) {

    $get_answers = $this->db->get_where('answers', array('question_id' => $row['question_id']), 4);
}
//push all the get_answers data into $query array as a subarray
return $query;
4

3 回答 3

1

我不认为你的 $query 变量实际上是一个数组,而是一个结果集。你也应该把它放到一个数组中。也许是这样的:

$query = $this->db->get('questions');
$result_array = array();
$i = 0;
foreach($query->result_array() as $row){
    $result_array[$i] = $row;
    $result_array[$i]['answers'] = $this->db->get_where('answers', array('question_id'=>$row['question_id']), 4);
    $i++;
}

var_dump($result_array);
于 2012-08-10T16:15:56.540 回答
0

用于array_merge()合并两个数组。

只需将其添加到 $query:

$query[] = $get_answers;
于 2012-08-10T16:10:28.667 回答
0

这是你想要做的吗?

$query = $this->db->get('questions');
$answers = array();

foreach($query->result_array() as $row){
    $get_answers = $this->db->get_where('answers', array('question_id'=>$row['question_id']), 4);

    $answers[] = $get_answers;
}

然后,$answers[]可以使用. $query_resultarray_merge()

$returnArray = array_merge($answers, $query);

return $returnArray;
于 2012-08-10T16:13:05.873 回答