2012-04-25 111 views

回答

3

你可以使用正则表达式替换SER每个号码前面加上数字:preg_replace('/(\d+)/', 'SER$1', '1,2,11,17,2');

+0

这样做伎俩!,很容易,但很明显! +1给@ jhon-himmelman快速。 – 2012-04-25 16:58:10

1

您可以使用array_map遍历数组并预先填充字符串。

例子:

$array = array(1,2,11,17,2); 

$new_array = array_map(function($value) { 
    return 'STR' . $value; 
}, $array); 

var_dump($new_array); 

编辑:无视,我还以为你使用数组。

1

对第一个'SER'使用连接,然后使用替换功能。

$StringVar="SER".$StringVar 
$StringVar=str_replace(" ,",", SER",$StringVar) 
0

在array_walk的例子,你看到这个代码:

<?php 
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"); 

function test_alter(&$item1, $key, $prefix) 
{ 
    $item1 = "$prefix: $item1"; 
} 

function test_print($item2, $key) 
{ 
    echo "$key. $item2<br />\n"; 
} 

echo "Before ...:\n"; 
array_walk($fruits, 'test_print'); 

array_walk($fruits, 'test_alter', 'fruit'); 
echo "... and after:\n"; 

array_walk($fruits, 'test_print'); 
?> 

上面的例子将输出:

Before ...: 
d. lemon 
a. orange 
b. banana 
c. apple 
... and after: 
d. fruit: lemon 
a. fruit: orange 
b. fruit: banana 
c. fruit: apple 
+0

你将不得不将字符串转换为数组,首先使用explode(); – MB34 2012-04-25 15:12:54