2016-05-16 123 views
3

我有包含这些值的数据阵列:如何用数组中的字符串替换子字符串使用preg_replace? (PHP)

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" 
+0

看看[array_walk()](http://php.net/manual/en/function.array-walk.php)或[array_reduce()](http://php.net) /manual/en/function.array-reduce.php)构建一个单一的字符串。 –

+0

你为什么要这样的结果?它是为了什么? –

+0

为什么你需要改变字符串,为什么不从数组中新的字符串? –

回答

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" 
+0

谢谢。我接受你的问题,因为我认为这是最好的答案。尽管如此,弗雷恩在评论中的建议是我目前的解决方案。 –

+1

是的,如果您正在处理真实代码(生产),Frayne的建议将非常合理。但是我已经将解决​​方案作为当前“抽象”测试案例的解决方案。无论如何,谢谢 – RomanPerekhrest

+0

这很棒,我也使用一些PHP库函数做出答案。 –

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 
) 
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)开头为“myid”,(1)为“id”,'? – splash58

+1

哈哈哈,有很多问题,但OP的要求已经解决了。 –

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" 
+0

复杂的方式,但你得到它。 +1 –

+0

@RaviHirani,实际上有一个人使用'str_pad'和'str_replace',另一个人'preg_replace_callback',所以左边是'foreach'。 –

相关问题