Using preg_replace on an array

前端 未结 3 433
孤独总比滥情好
孤独总比滥情好 2021-01-13 07:55

I have a relatively large array of elements which I want to search for a string and replace any matches. I\'m currently trying to do this using preg_replace and

3条回答
  •  隐瞒了意图╮
    2021-01-13 08:53

    Your value does not sit in the array as a simple element but as a subset right? Like so?

    array (
      array ('id' => 45, 'name' => 'peter', 'whatyouarelookingfor' => '5F'),
      array ('id' => 87, 'name' => 'susan', 'whatyouarelookingfor' => '8C'),
      array ('id' => 92, 'name' => 'frank', 'whatyouarelookingfor' => '9R')
    )
    

    if so:

     $value) {
      $array[$key]['whatyouarelookingfor'] =  
        preg_replace("/\d?\dIPT\.\w/", "IPT", $value['whatyouarelookingfor']);
    }
    

    Or if more elements have this, just go broader:

     $value) {
      $array[$key] =  
        preg_replace("/\d?\dIPT\.\w/", "IPT", $value);
    }
    

提交回复
热议问题