我有一个名为$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 个元素)数组元素中的这个元素。
您需要在
循环中获取索引并在那里设置值,或者只在 for 循环中使用引用。默认情况下,for 循环中的所有项都是其原始数组的副本,因此不会反映对其迭代元素所做的更改。
通过索引
foreach ($topic_notes as $idx => $topic)
{
// SNIP
$topic_notes[$idx]['subject'] = $subject_name;
}
通过参考
foreach ($topic_notes as &$topic)
{
// SNIP
$topic['subject'] = $subject_name;
}
在您的原始示例中,您也没有操纵子数组,而是操作父数组!
您最好在第一个查询 (JOIN) 中从数据库中获取所有这些内容,但如果这是不可能的,请使用引用,然后更改它:
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);
$topic['subject'] = $topic_subject['subject_name'];
}
试试这个:
foreach ($topic_notes as $index => $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[$index]['subject'] = $subject_name;
}
你的意思是:
foreach ($topic_notes as $key => $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[$key]['subject'] = $subject_name;
}
旁白:小心字符串连接 - 那里有一个很好的SQL注入错误等待发生......