2012-04-25 17 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