多维数组中我的数组的所有值都包含一个变量+ string。
如何在我的示例中更改array[2]
和array[3]
的所有值?
我想用$var
更改名为$var2
的变量,并在$between
和字符串之间放置另一个名为$var2
的变量。
实施例
$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-";
结果
$array[2]=array("{$var2}{$between}end1","{$var2}{$between}end2","{$var2} {$between}end3");
$array[3]=array("{$var2}{$between}end1","{$var2}{$between}end2","{$var2} {$between}end3");
更新:首先我只有一维数组,而maxhud的答案对于这种情况来说是完美的。
答案 0 :(得分:4)
$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);
}
}