我有包含這些值的數據陣列:如何用數組中的字符串替換子字符串使用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"
看看[array_walk()](http://php.net/manual/en/function.array-walk.php)或[array_reduce()](http://php.net) /manual/en/function.array-reduce.php)構建一個單一的字符串。 –
你爲什麼要這樣的結果?它是爲了什麼? –
爲什麼你需要改變字符串,爲什麼不從數組中新的字符串? –