簡體   English   中英

如何將每個數組項的數組轉換為帶有占位符的逗號分隔的字符串?

[英]How to convert an array to comma-separated string with placeholders for each array item?

我有一個變量$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 = "?, ?, ?, ?, ?"

嘗試這個:

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

您可以這樣做。

https://eval.in/401410

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM