我有一个数组:
$array = array(
"peter",
"john",
"maria",
"bob");
现在我需要以某种方式将数组更改为字符串(按 asc 排序),例如:
$string_with_commas = "bob, john, maria, peter";
我有一个数组:
$array = array(
"peter",
"john",
"maria",
"bob");
现在我需要以某种方式将数组更改为字符串(按 asc 排序),例如:
$string_with_commas = "bob, john, maria, peter";
<?php
$array = array('lastname', 'email', 'phone');
sort($array)
$comma_separated = implode(",", $array);
echo $comma_separated; // email,lastname,phone
?>
$array = array("peter", "john", "maria", "bob");
sort($array);
$string_with_commas = implode(', ', $array);
<?php
sort($array);
echo implode(', ', $array);
sort()
将修改您的阵列。如果你不想要那个,你必须复制那个数组。
<?php
$sorted_array = $array;
sort($sorted_array);
sort($array); //sort array in ASC order
$string = implode(', ', $array); //store as comma separated string
只需这样做:
$string_with_commas = implode(', ', $array);