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

add a new named key into matches of the preg_match_all

我的原码是:

$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

有什么想法吗?

<?php

$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)
{
   $rov['batch'] = $match;
   $rov['type'] = 1;
   $rov['scid'] = 9;
   $res[] = $rov;

}

print_r($res);

?>

您可以使用 array_map() 到 "extend" 每个匹配项:

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