为什么我没有在以下场景中获得更新的阵列?

时间:2013-11-25 11:51:20

标签: php arrays multidimensional-array foreach associative-array

我有一个名为$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);

1 个答案:

答案 0 :(得分:2)

$ 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会更加优雅,并执行数组diff来获取$ 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 );
}