8

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 regular expressions:

preg_replace("/\d?\dIPT\.\w/", "IPT", $array);

I want to get all values which match either 00IPT.A or 0IPT.A (with 0 representing any numerical character and A representing any letter) and replace them with IPT. However, I'm getting array to string conversion notices. Is there any way to get preg_replace to accept an array data source? If not, is there any other way I could achieve this?

The documentation says that preg_replace should be able to accept array sources — this is the reason I'm asking.

The string or an array with strings to search and replace. If subject is an array, then the search and replace is performed on every entry of subject, and the return value is an array as well.

The array is multidimensional if that helps (has multiple arrays under one main array).

1

3 Answers 3

16

preg_replace doesn't modify in place. To permanently modify $array, you simply need to assign the result of preg_replace to it:

$array = preg_replace("/\d{1,2}IPT\.\w/", "IPT", $array);

works for me.

$array = array('00IPT.A', '0IPT.A');
$array = preg_replace("/\d{1,2}IPT\.\w/", "IPT", $array);
var_dump($array);
// output: array(2) { [0]=> string(3) "IPT" [1]=> string(3) "IPT" }

Note: the \d{1,2} means one or two digits.

If you want to do this to a two-dimensional array, you need to loop through the first dimension:

$array = array( array('00IPT.A', 'notmatch'), array('neither', '0IPT.A') );    
foreach ($array as &$row) {
    $row = preg_replace("/\d{1,2}IPT\.\w/", "IPT", $row);
}
var_dump($array);

output:

array(2) { 
    [0]=> array(2) { 
        [0]=> string(3) "IPT" 
        [1]=> string(8) "notmatch" 
    } 
    [1]=> &array(2) { 
        [0]=> string(7) "neither" 
        [1]=> string(3) "IPT" 
    } 
}

Note that you have to loop through each row by reference (&$row) otherwise the original array will not be modified.

Sign up to request clarification or add additional context in comments.

3 Comments

Would this work on multidimensional arrays though? My arrays are organised like this: array(4500) { [0]=> array(19) { [0]=> string(0) "" [1]=> string(32) "SNIP" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(0) "" [5]=> string(0) "" [6]=> string(0) "" }
And which elements to you want to perform the replacement on? All of them?
I'd like to replace the values of all elements which match the regex search term. Some arrays after the one I listed include: string(5) "9IPT.A" [4]=> string(3) "TEA" [5]=> string(2) "BT" [6]=> string(5) "10RC1" [7]=> string(3) "IRE" [8]=> string(2) "A3" [9]=> string(5) "10RC1" So an example would be leaving 10RC1 untouched but changing 9IPT.A to IPT. Would this be possible?
1

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:

<?php

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

Or if more elements have this, just go broader:

<?php

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

Comments

1

your $array contains some further arrays. preg_replace works fine with arrays of strings, but not with arrays of arrays [of arrays...] of strings.

1 Comment

Oh so preg_replace doesn't work with multidimensional arrays? Dam, do you know a way around this then?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.