Reputation: 80
All values of my arrays in a multidimensional array consists of one variable + string.
How can I change all values from array[2]
and array[3]
in my example?
I want to change the variable called $var
with $var2
and put another variable called $between
between $var2
and the string.
Example
$var = "start-";
$array = array
(
array("{$var}end1a","{$var}end2a","{$var}end3a"),
array("{$var}end1b","{$var}end2b","{$var}end3b"),
array("{$var}end1c","{$var}end2c","{$var}end3c")
);
$var2 = "new-start-";
$between = "between-";
result
$array[2]=array("{$var2}{$between}end1","{$var2}{$between}end2","{$var2} {$between}end3");
$array[3]=array("{$var2}{$between}end1","{$var2}{$between}end2","{$var2} {$between}end3");
UPDATE: First I had only an one-dimensional array and the answer from maxhud was perfect for this case.
Upvotes: 0
Views: 115
Reputation: 10206
$start = "start-";
$array = array(
array($start."enda1", $start."enda2", $start."enda3"),
array($start."endb1", $start."endb2", $start."endb3"),
array($start."endc1", $start."endc2", $start."endc3")
);
$newStart = "new-start-";
$between = "between-";
$keys = array(2, 5);
foreach ($keys as $key) {
foreach ($array[$key] as $key2 => $value) {
$array[$key][$key2] = str_replace($start, $newStart.$between, $value);
}
}
Upvotes: 4