Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through array updating values PHP

Tags:

arrays

loops

php

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

like image 292
Belgin Fish Avatar asked Oct 14 '25 23:10

Belgin Fish


1 Answers

foreach ($info as $key => $value)
  $info[$key] = str_replace('%value%', 'MyValue', $value);

Demo: http://ideone.com/65F3L

like image 131
mellamokb Avatar answered Oct 17 '25 13:10

mellamokb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!