如何使用 preg_replace 替换子字符串为数组中的字符串?(PHP)

3
我有一个数据数组包含以下数值:

Array
(
    [id] => 1
    [myid] => 9
    [date] => 2014-01-30
    [user] => 17
    [reason] => some text here...
)

这个字符串包含数字,指向数据数组的索引:

$columns = '(0) as "id",(1) as "myid",(2) as "date",(3) as "user",(4) as "reason"';

是否可以将括号中的数字(包括括号)更改为数组中适当的值?

我知道如何使用(string) preg_replace( (array) $patterns, (array) $replacements, (string) $subject),但不太清楚如何解决这个问题。

理想情况下,结果字符串应该像这样:

'1' as "id",'9'  as "myid",'2014-01-30'  as "date",'17'  as "user",'some text here...'  as "reason"

请查看 array_walk()array_reduce() 来构建一个单一的字符串。 - Pinke Helga
你为什么想要那个结果?它有什么用途? - Pinke Helga
为什么你需要改变字符串,而不是从数组中创建新的字符串? - Murad Hasan
Quasimodo的克隆:对于Sqlite查询@Frayne Konok:我可以这样做,这将是最简单的方法。 - John Boe
在你的编辑后,事实证明一个简单的 preg_replace_callback 最适合你的需求。 - Pinke Helga
显示剩余2条评论
5个回答

2
你可以使用简单的foreach循环来实现:
$info = array(
    'id' => 1,
    'myid' => 9,
    'date' => '2014-01-30',
    'user' => 17,
    'reason' => 'some text here...'
);

$columns = '(0) as "id",(1) as "myid",(2) as "date",(3) as "user",(4) as "reason"';

foreach (array_values($info) as $key => $value) {
    $columns = str_replace(
        '(' . $key . ')',
        str_pad($value, strlen($value) + 2, "'", STR_PAD_BOTH),
        $columns
    );
}

echo $columns;

1
如果字符串以 $columns = '(0) as "myid",(1) as "id", 开头会怎样? - splash58
1
哈哈哈,有很多问题,但是原帖的要求已经解决了。 - Murad Hasan

2

使用preg_replace_callbackstr_replace函数的解决方案:

$columns = '(0) as "id",(1) as "myid",(2) as "date",(3) as "user",(4) as "reason"';

// $arr is your initial array
$result = preg_replace_callback(
                "/(\(\d\)) as \"(\w+?)\",?/i",
                function ($maches) use($arr){
                     return str_replace([$maches[1]], "'". $arr[$maches[2]]. "'", $maches[0]);
                },
                $columns);

print_r($result);

输出结果:
'1' as "id",'9' as "myid",'2014-01-30' as "date",'17' as "user",'some text here...' as "reason"

谢谢。我接受你的问题,因为我认为这是最好的答案。尽管Frayne在评论中的建议是我的当前解决方案。 - John Boe
1
是的,如果你正在处理真正的代码(生产),Frayne的建议将是相当合理的。但我已经提出了这个解决方案作为你当前“抽象”测试用例的解决方案。不管怎样,谢谢。 - RomanPerekhrest
太好了,我也使用一些PHP库函数来编写答案。 - Murad Hasan

1
你可以使用一些PHP库函数来完成它。
$info = array(
    'id' => 1,
    'myid' => 9,
    'date' => '2014-01-30',
    'user' => 17,
    'reason' => 'some text here...'
);

$columns = '(0) as "id",(1) as "myid",(2) as "date",(3) as "user",(4) as "reason"';

$new_Arr = array();
$column_arr = explode(",", $columns);
foreach($column_arr as $values){
    $arr = explode(" as ", $values);
    $new_Arr[] = "'".$info[trim($arr[1], '"')]."' as ".$arr[1];
}

echo implode(",", $new_Arr) //'1' as "id",'9' as "myid",'2014-01-30' as "date",'17' as "user",'some text here...' as "reason"

复杂的方式,但你懂了。+1 - Ravi Hirani
@RaviHirani,实际上有一个人使用了str_padstr_replace完成了它,另一个人使用了preg_replace_callback,所以剩下的就是foreach - Murad Hasan

0

只需使用带有 str_replace 的循环。遍历数组,使用循环索引作为搜索字符串,并将其替换为相应的值。


0

array_flip 是你的救星

直接来自文档

<?php
$input = array("oranges", "apples", "pears");
$flipped = array_flip($input);

print_r($flipped);
?>

上述示例将输出:

Array
(
    [oranges] => 0
    [apples] => 1
    [pears] => 2
)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接