Reputation: 19837
say I have an array in php like this
$info['name'] = 'test %value%';
$info['city'] = 'city test %value%';
$info['other'] = '%value% city test';
all I want to do is loop through this array and replace all the instances of %value% with a supplied string, saving it into the same array.
What would be the best way to do that? :)
Thanks
Upvotes: 4
Views: 8211
Reputation: 141839
This seems to be the cleanest way to me, but it requires PHP 5.3 or higher:
$info = array_map(function($x) use ($newValue){
return str_replace('%value%', $newValue, $x);
}, $info);
Upvotes: 4
Reputation: 56769
foreach ($info as $key => $value)
$info[$key] = str_replace('%value%', 'MyValue', $value);
Demo: http://ideone.com/65F3L
Upvotes: 15