我有一个名为 $questions
的数组,如下所示:
Array
(
[0] => Array
(
[question_id] => 33185
[question_parent_id] => 0
[question_subject_id] => 4
[question_topic_id] => 503
[question_directions] =>
[question_text] => Two gases are at 300 K and 350 K respectively Ratio of average kinetic energy of their molecules is
[question_file] =>
[question_description] =>
[question_difficulty_type] => 1
[question_has_sub_ques] => 0
[question_picked_individually] => no
[question_appeared_count] => 0
[question_manual] => 0
[question_site_id] =>
[question_created_staff_id] => fbfee12504bf3c4a038d4c9f142f894e
[question_added_date] => 1328180210
[question_updated_staff_id] =>
[question_updated_date] => 0
)
[1] => Array
(
[question_id] => 33187
[question_parent_id] => 0
[question_subject_id] => 4
[question_topic_id] => 503
[question_directions] =>
[question_text] => what will be the temperature when the rms velocity is double the rms velocity at 300 K
[question_file] =>
[question_description] =>
[question_difficulty_type] => 1
[question_has_sub_ques] => 0
[question_picked_individually] => no
[question_appeared_count] => 0
[question_manual] => 0
[question_site_id] =>
[question_created_staff_id] => fbfee12504bf3c4a038d4c9f142f894e
[question_added_date] => 1328180274
[question_updated_staff_id] =>
[question_updated_date] => 0
)
[2] => Array
(
[question_id] => 33188
[question_parent_id] => 0
[question_subject_id] => 4
[question_topic_id] => 503
[question_directions] =>
[question_text] => a gas at 300 K has pressure 4 × 10-10 N/m 2 If k = 1.38 × 10-23 J/K the number of molecules./ cm3 of the order of
[question_file] =>
[question_description] =>
[question_difficulty_type] => 1
[question_has_sub_ques] => 0
[question_picked_individually] => no
[question_appeared_count] => 0
[question_manual] => 0
[question_site_id] =>
[question_created_staff_id] => fbfee12504bf3c4a038d4c9f142f894e
[question_added_date] => 1328180400
[question_updated_staff_id] => 1096ab29ecde5cec198bb2ebe730d229
[question_updated_date] => 1338272917
)
[3] => Array
(
[question_id] => 33190
[question_parent_id] => 0
[question_subject_id] => 4
[question_topic_id] => 503
[question_directions] =>
[question_text] => The rms speed of oxygen molecules at a certain temperature is v if the temperature is doubled and the oxygen gas dissociates into atomic oxygen, the rms speed would be
[question_file] =>
[question_description] =>
[question_difficulty_type] => 1
[question_has_sub_ques] => 0
[question_picked_individually] => no
[question_appeared_count] => 0
[question_manual] => 0
[question_site_id] =>
[question_created_staff_id] => fbfee12504bf3c4a038d4c9f142f894e
[question_added_date] => 1328180486
[question_updated_staff_id] => 1096ab29ecde5cec198bb2ebe730d229
[question_updated_date] => 1338273032
)
)
我编写了以下代码来更新数组。但是在 foreach 循环执行完成后,如果我打印数组 $questions 那么我不会得到更新的数组。我得到的是原始数组。谁能在这方面指导我解决我的问题?任何帮助将不胜感激。以下是我的数组更新代码供您引用:
$sql = " SELECT * FROM ".TBL_QUESTIONS." WHERE question_subject_id=".$subject_id;
$sql .= " AND question_topic_id=".$topic_id;
$this->mDb->Query($sql);
$questions = $this->mDb->FetchArray();
$exclude_words = array('at','is','are','when','whom');
foreach($questions as $arr) {
foreach($exclude_words as $excluding) {
$str_start_pos = strpos($arr['question_text'], $excluding);
if($str_start_pos >= 0) {
if($str_start_pos != 0)
$excluding = " ".$excluding;
$excluding = $excluding." ";
$arr['question_text'] = str_replace($excluding, "", $arr['question_text']);
}
}
}
print_d($questions);
最佳答案
$questions 不是通过引用传递到 $arr 中。因此,您必须在每个循环上显式更新 $questions
foreach($questions as $index=>$arr) {
foreach($exclude_words as $excluding) {
$str_start_pos = strpos($arr['question_text'], $excluding);
if($str_start_pos >= 0) {
if($str_start_pos != 0) $excluding = " ".$excluding;
$excluding = $excluding." ";
$arr['question_text'] = str_replace($excluding, "", $arr['question_text']);
$questions[$index]['question_text']=$arr['question_text'];
}
}
}
这是一个快速但肮脏的解决方案。我相信按空格分解 $question 并执行数组比较来获取 $questions_array 中不属于 $exclude_words 数组的元素会更优雅。最后,你使用空间作为粘合剂来内爆干净的数组。
foreach($questions as $index=>$arr) {
$questions_array = explode(' ',$arr['question_text']);
$clean_questions = array_diff($questions_array, $exclude_words);
$questions[$index]['question_text'] = implode(' ',$clean_questions );
}
关于php - 为什么我在以下场景中没有获得更新的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20192362/