到目前为止:我正在进行preg_match_all搜索,我的输出是:Donald,Daisy,Huey,Dewey和Louie
代码是这样的:
$duckburg = array();
preg_match_all($pattern,$subject,$match);
$duckburg['residents'] = $match[1];
print_r($duckburg['residents']);
输出:
Array ( [residents] => Array ( [0] => Donald [1] => Daisy [2] => Huey [3] => Dewey [4] => Louie )
我的问题:我想在每个字符串中添加“Duck”
使用此帮助字符串:$lastname = " Duck"
输出应为:
Array ( [residents] => Array ( [0] => Donald Duck [1] => Daisy Duck [2] => Huey Duck [3] => Dewey Duck [4] => Louie Duck )
我尝试过(但它不起作用):
preg_match_all($pattern,$subject,$match);
$matchy = $match.$lastname;
$duckburg['residents'] = $matchy[1];
print_r($duckburg['residents']);
是否可以在匹配字符串进入数组之前更改它?谢谢你的帮助!
答案 0 :(得分:0)
迭代数组是一种可能的选择:
$lastname = " Duck";
preg_match_all($pattern,$subject,$matches);
foreach($matches as $key => $val){
$duckburg['residents'][$key] = $val . $lastname;
}
print_r($duckburg['residents']);
答案 1 :(得分:0)
Array_map是这种操纵的工具:
$match = Array ( 'residents' => Array ('Donald','Daisy','Huey','Dewey','Louie'));
$duckburg['residents'] = array_map(function($n) { return "$n Duck"; }, $match['residents']);
var_dump($duckburg);
<强>输出:强>
array(1) {
["residents"]=>
array(5) {
[0]=>
string(11) "Donald Duck"
[1]=>
string(10) "Daisy Duck"
[2]=>
string(9) "Huey Duck"
[3]=>
string(10) "Dewey Duck"
[4]=>
string(10) "Louie Duck"
}
}