在以下情况下,如何从每个包含字符串的关联数组元素中删除几个字符串


How to remove few strings from each associative array element containing a string in following situation?

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

我有另一个名为$excluding_this的数组。打印此数组后,我们得到以下输出:

Array ( [0] => a [1] => at [2] => is [3] => are [4] => when [5] => whom )

现在我想做的是,我必须解析数组$questions的每个数组元素,并访问包含在键['question_text']中的字符串。在这个字符串中,我必须检查数组$excluding_this中的任何字符串是否存在于字符串(包含在['question_text']键中的字符串)中。如果['question_text']值中存在任何或所有字符串/s,则从键['question_text']的值中删除所有这些字符串,并且应该返回具有这些更改的['question_text']的新数组。我不知道我应该如何以最佳方式实现这一点。有谁能帮我解决这个问题吗?任何形式的帮助都将不胜感激。正在等待您的答复。

这就是我构建它的方式。在foreach过程中跳过排除的条目。所以你不必两次穿过你的大阵列。

foreach ( $array1 as $arr ) {
    foreach ( $excluding_this as $excludeString ) 
        if ( strpos($arr['question_text'], $excludeString ) 
            $arr['question_text'] = str_replace($excludeString, '', $arr['question_text']);
    ...
    // rest of the code
    ...
}

根据需要进行更改。。

$excluding_this = array("a","at","is","are","when","whom");
foreach($questions as $arr) {
    foreach($excluding_this as $excluding) {
        if(strpos($arr['question_text'],$excluding)) {
            $arr['question_text'] = str_replace($excluding, "", $arr['question_text'];
        }
    }
}