gpt4 book ai didi

php - 将新的命名键添加到 preg_match_all 的匹配项中

转载 作者:搜寻专家 更新时间:2023-10-31 21:08:12 24 4
gpt4 key购买 nike

我的原始代码是:

$sc = 'hello 8491241 some text 6254841 some text 568241';
preg_match_all('/[0-9]{5,10}/', $sc, $matches1);

$all_matches = $matches1[0];

foreach ($all_matches as $match)
{
$sid = '9';

$rov['batch'] = $match;
$rov['scid'] = $sid;
$res[] = $rov;
}

print_r($res);

我如何将新的命名键 ['type'] 添加到 preg_match_all 的 $matches1 中以在 foreach 中调用它并给我最终输出:

Array
(
[0] => Array
(
[batch] => 8491241
[type] => 1
[scid] => 9
)

[1] => Array
(
[batch] => 568241
[type] => 1
[scid] => 9
)

[2] => Array
(
[batch] => 6254841
[type] => 1
[scid] => 9
)
)

我尝试的是:

$sc = 'hello 8491241 some text 6254841 some text 568241';
preg_match_all('/[0-9]{5,10}/', $sc, $matches1);

$pr_matches1['batch'] = $matches1[0];
$pr_matches1['type'] = 1;
$all_matches[] = $pr_matches1;

foreach ($all_matches as $match)
{
$sid = '9';

$rov['batch'] = $match['batch'];
$rov['type'] = $match['type'];
$rov['scid'] = $sid;
$res[] = $rov;
}

print_r($res);

但它给我错误的输出 http://pastebin.com/WXGpLTX9

有什么想法吗?

最佳答案

你可以使用 array_map() “扩展”每个匹配项:

$all_matches = array_map(function($match) {
return [
'batch' => $match,
'type' => 1,
'scid' => 9,
];
}, $matches1[0]);

关于php - 将新的命名键添加到 preg_match_all 的匹配项中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28620563/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com