How to remove few strings from each associative ar

2019-06-11 06:38发布

问题:

I'm having an associative array named $questions. For your reference I'm printing out first four elements from this array. The actual array is quite large but all the array elements are similar as the elements printed below:

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
        )
)

I'm having another array named $excluding_this. After printing this array we get following output:

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

Now what I want to do is, I've to parse through each array element of the array $questions and access the the string contained in a key ['question_text']. In this string I've to check whether any of the string from the array $excluding_this is present within the string(the string contained in ['question_text'] key) or not. If any or all of the string/s is/are present in the ['question_text'] value then remove all those strings from the value of key ['question_text'] and a new array with these changed ['question_text'] should get returned. I'm not getting how should I achieve this in optimum way. Can any one please help me out in this issue? Any kind of help would be highly appreciated. Waiting for your reply.

回答1:

This is how I would build it. Skipping the excluded entries during the foreach. So you dont have to go through your big array twice.

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
    ...

}

Alter where needed..



回答2:

$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'];
        }
    }
}