0

我有一个数组:

$array = array(
"peter",
"john",
"maria",
"bob"); 

现在我需要以某种方式将数组更改为字符串(按 asc 排序),例如:

$string_with_commas = "bob, john, maria, peter";
4

5 回答 5

3
<?php

$array = array('lastname', 'email', 'phone');
sort($array)
$comma_separated = implode(",", $array);

echo $comma_separated; // email,lastname,phone

?>

http://php.net/manual/en/function.implode.php

http://php.net/manual/en/function.sort.php

于 2012-09-15T16:37:15.417 回答
2
$array = array("peter", "john", "maria", "bob"); 

sort($array);

$string_with_commas = implode(', ', $array);
于 2012-09-15T16:39:30.757 回答
1
<?php
sort($array);
echo implode(', ', $array);

sort()将修改您的阵列。如果你不想要那个,你必须复制那个数组。

<?php
$sorted_array = $array;
sort($sorted_array);
于 2012-09-15T16:38:17.977 回答
1
sort($array); //sort array in ASC order
$string = implode(', ', $array); //store as comma separated string
于 2012-09-15T16:38:45.400 回答
0

只需这样做:

$string_with_commas = implode(', ', $array);
于 2012-09-15T16:38:21.867 回答