Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String replace all items in array PHP

I would like to do a string replacement in all items in an array. What I have is:

$row['c1'] = str_replace("&", "&", $row['c1']);
$row['c2'] = str_replace("&", "&", $row['c2']);
$row['c3'] = str_replace("&", "&", $row['c3']);
$row['c4'] = str_replace("&", "&", $row['c4']);   
$row['c5'] = str_replace("&", "&", $row['c5']);
$row['c6'] = str_replace("&", "&", $row['c6']);
$row['c7'] = str_replace("&", "&", $row['c7']);   
$row['c8'] = str_replace("&", "&", $row['c8']);
$row['c9'] = str_replace("&", "&", $row['c9']);
$row['c10'] = str_replace("&", "&", $row['c10']); 

How can I achieve this with less code? I thought a foreach statement would work, e.g.:

$columns = array($row['c1'], $row['c2'], $row['c3'], $row['c4'], $row['c5'], $row['c6'], $row['c7'], $row['c8'], $row['c9'], $row['c10']);

foreach ( $columns as $value){
   $value   = str_replace("&", "&", $value);
}

But it doesn't work.

like image 375
AJFMEDIA Avatar asked Feb 18 '11 18:02

AJFMEDIA


People also ask

How to replace value in array PHP?

The array_replace() function replaces the values of the first array with the values from following arrays. Tip: You can assign one array to the function, or as many as you like. If a key from array1 exists in array2, values from array1 will be replaced by the values from array2.

How to replace in string PHP?

The str_replace() function replaces some characters with some other characters in a string. This function works by the following rules: If the string to be searched is an array, it returns an array. If the string to be searched is an array, find and replace is performed with every array element.

How can I replace multiple characters in a string in PHP?

Approach 1: Using the str_replace() and str_split() functions in PHP. The str_replace() function is used to replace multiple characters in a string and it takes in three parameters. The first parameter is the array of characters to replace.


3 Answers

Just do:

$row = str_replace("&", "&", $row);

Note: Your foreach doesn't work because you need a reference, or use the key:

foreach ( $columns as &$value) { // reference
   $value  = str_replace("&", "&", $value);
}
unset($value); // break the reference with the last element

Or:

foreach ($columns as $key => $value){
   $columns[$key]  = str_replace("&", "&", $value);
}

Although it is not necessary here because str_replace accepts and returns arrays.

like image 96
netcoder Avatar answered Sep 17 '22 16:09

netcoder


You should call it by reference, otherwise foreach creates a duplicate copy of $value

foreach ( $columns as &$value)

like image 20
Ish Avatar answered Sep 19 '22 16:09

Ish


Another solution to is to use PHP array_walk like this:

function custom_replace( &$item, $key ) {
   $item = str_replace('22', '75', $item);
} 

// Init dummy array.
$columns = array('Cabbage22', 'Frid22ay', 'Internet', 'Place22', '22Salary', '22Stretch', 'Whale22Inn');

// Print BEFORE.
echo 'Before: ';
print_r($columns);

// Make the replacements.
array_walk($columns, 'custom_replace');

// Print AFTER.
echo 'After:';
print_r($columns);

Output:

Before: Array
(
    [0] => Cabbage22
    [1] => Frid22ay
    [2] => Internet
    [3] => Place22
    [4] => 22Salary
    [5] => 22Stretch
    [6] => Whale22Inn
)
After: Array
(
    [0] => Cabbage75
    [1] => Frid75ay
    [2] => Internet
    [3] => Place75
    [4] => 75Salary
    [5] => 75Stretch
    [6] => Whale75Inn
)
like image 41
mr.baby123 Avatar answered Sep 20 '22 16:09

mr.baby123