【问题标题】:How to remove few strings from each associative array element containing a string in following situation?在以下情况下,如何从包含字符串的每个关联数组元素中删除几个字符串?
【发布时间】:2013-11-25 10:33:54
【问题描述】:

我有一个名为 $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'] 值中存在任何或所有字符串,则从键['question_text'] 的值中删除所有这些字符串,并且应该返回具有这些更改的['question_text'] 的新数组。 我不知道我应该如何以最佳方式实现这一目标。有人可以帮我解决这个问题吗? 任何形式的帮助将不胜感激。等待您的回复。

【问题讨论】:

    标签: php arrays string associative-array key-value


    【解决方案1】:

    这就是我将如何构建它。在 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
        ...
    
    }
    

    在需要的地方改变..

    【讨论】:

      【解决方案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'];
              }
          }
      }
      

      【讨论】:

      • 不错的副本,但在 strpos($arr['question_text'],$excluding_this) $ exclude_this 将被转换为 int。 (引自文档:“如果 needle 不是字符串,则将其转换为整数并用作字符的序数值。”)
      • 首先,我没有抄袭你的任何东西。我自己写并不难,因为我第一次使用 in_array 但后来意识到我的错,改为 strpos 但没有意识到它需要是一个字符串,我的错,将编辑。
      猜你喜欢
      • 1970-01-01
      • 2023-02-06
      • 1970-01-01
      • 2020-05-29
      • 2020-05-18
      • 2019-11-20
      • 2011-07-14
      • 2022-09-23
      • 1970-01-01
      相关资源
      最近更新 更多