2015-07-20 26 views
1

我有一個變量$input,其中包含具有可變數量項目的數組。如何使用每個數組項的佔位符將數組轉換爲逗號分隔的字符串?

如何創建一個逗號分隔此字符串顯示在下面的例子周邊的每一個問號每個值和引號一個問號?

我嘗試使用以下但這種包裝所有問號在一個引號代替具有圍繞每個問號引號:

我嘗試:

$output = implode(",", array_fill(0, count($input), "?")); 

例陣列:

array(5) { 
    [0]=> 
    string(1) "1" 
    [1]=> 
    string(1) "2" 
    [2]=> 
    string(1) "3" 
    [3]=> 
    string(1) "4" 
    [4]=> 
    string(1) "5" 
} 

預計產量:

$output = "?", "?", "?", "?", "?" 

電流輸出:

$output = "?, ?, ?, ?, ?" 
+0

使用'loop' ... –

+1

'$輸出=破滅( 「」 array_fill(0,計數($輸入), 「?」 ));'不會將任何內容包含在引號中....代碼中的其他內容必須將引號放在$ output附近;但如果你需要使用引號'$輸出=破滅( ' 「」',array_fill(0,計數($輸入)) 「?」);' –

+0

@MarkBaker:謝謝。這只是爲了顯示當前和預期產出之間的差異,對不起。我的意思是我需要每個問號周圍的引號。 – TaneMahuta

回答

4

試試這個:

$output = implode(", ", array_fill(0, count($input), "\"?\"")); 
+0

非常感謝 - 這個偉大工程。我會盡快接受。 :) – TaneMahuta

+0

很高興能幫到你! – Ben

1

你可以不喜歡這樣。

https://eval.in/401410

CODE

<?php 
$K= array("1","2","3","4","5"); 
$P=preg_filter('/^(.*)/', '"?"', $K); 
echo implode(",",$P); 
?> 
相關問題