我有一个命名$topic_notes
如下的数组:
Array
(
[0] => Array
(
[topic_id] => 214
[topic_subject_id] => 4
[topic_notes] => Nice Story
)
[1] => Array
(
[topic_id] => 215
[topic_subject_id] => 4
[topic_notes] =>
)
[2] => Array
(
[topic_id] => 216
[topic_subject_id] => 4
[topic_notes] =>
)
[3] => Array
(
[topic_id] => 217
[topic_subject_id] => 4
[topic_notes] =>
)
[4] => Array
(
[topic_id] => 218
[topic_subject_id] => 4
[topic_notes] =>
)
[5] => Array
(
[topic_id] => 219
[topic_subject_id] => 4
[topic_notes] =>
)
[6] => Array
(
[topic_id] => 220
[topic_subject_id] => 4
[topic_notes] =>
)
[7] => Array
(
[topic_id] => 221
[topic_subject_id] => 4
[topic_notes] =>
)
[8] => Array
(
[topic_id] => 223
[topic_subject_id] => 4
[topic_notes] =>
)
[9] => Array
(
[topic_id] => 504
[topic_subject_id] => 4
[topic_notes] =>
)
[10] => Array
(
[topic_id] => 225
[topic_subject_id] => 4
[topic_notes] =>
)
)
现在我想在数组中存在的每个内部数组中创建一个新的键值对,$topic_notes
但我做不到。我试过的代码如下:
foreach ($topic_notes as $topic)
{
$sql = "SELECT subject_name FROM ".TBL_SUBJECTS." WHERE subject_id=".$topic['topic_subject_id'];
$gDb->Query($sql);
$topic_subject = $gDb->FetchArray(MYSQL_FETCH_SINGLE);
$subject_name = $topic_subject['subject_name'];
$topic_notes['subject'] = $subject_name;
}
执行此代码后,我得到以下输出:
Array
(
[0] => Array
(
[topic_id] => 214
[topic_subject_id] => 4
[topic_notes] => Nice Story
)
[1] => Array
(
[topic_id] => 215
[topic_subject_id] => 4
[topic_notes] =>
)
[2] => Array
(
[topic_id] => 216
[topic_subject_id] => 4
[topic_notes] =>
)
[3] => Array
(
[topic_id] => 217
[topic_subject_id] => 4
[topic_notes] =>
)
[4] => Array
(
[topic_id] => 218
[topic_subject_id] => 4
[topic_notes] =>
)
[5] => Array
(
[topic_id] => 219
[topic_subject_id] => 4
[topic_notes] =>
)
[6] => Array
(
[topic_id] => 220
[topic_subject_id] => 4
[topic_notes] =>
)
[7] => Array
(
[topic_id] => 221
[topic_subject_id] => 4
[topic_notes] =>
)
[8] => Array
(
[topic_id] => 223
[topic_subject_id] => 4
[topic_notes] =>
)
[9] => Array
(
[topic_id] => 504
[topic_subject_id] => 4
[topic_notes] =>
)
[10] => Array
(
[topic_id] => 225
[topic_subject_id] => 4
[topic_notes] =>
)
[subject] => 12 PHYSICS
)
新的键值对(即新的数组元素)到达最后一个位置。实际上,我希望在每个(所有 10 个元素)数组元素中都有这个元素。