放大不显示结果PHP

时间:2018-09-17 11:46:42

标签: php string

我有一个像这样的简单代码:-

  $array = array(0 => 47, 1 => 51);
    foreach($array as $key => $gg1){
       $commaList = implode(', ', $gg1);
        echo $commaList;
    }

但是它没有显示我想要的输出:-

 '47', '51'

2 个答案:

答案 0 :(得分:2)

如果您正在使用implode函数,则无需循环。从documentation

  

返回一个字符串,其中包含所有数组的字符串表示形式   元素以相同的顺序排列,每个元素之间都有粘合线。

只需执行以下操作:

// Your input array
$input = array([0] => 47, [1] => 51);

// Simply use implode function to convert an array to 
// a string where array values are separated by a delimiter (',')
$commaList = implode(', ', $input);

echo $commaList; // prints out 47, 51

// Based on OP's comments, add double quotes also
$commaDoubleQuoteList = '"' . implode('", "' , $input) . '"';

echo $commaDoubleQuoteList; // prints out "47", "51"

// Based on OP's edit, add single quotes
// notice the reversal of " with '
$commaSingleQuoteList = "'" . implode("', '" , $input) . "'";

echo $commaSingleQuoteList; // prints out '47', 51'

答案 1 :(得分:1)

您不需要循环尝试

$array = array(0 => 47, 1 => 51);
$commaList = implode(', ', $array);
echo $commaList;