将多维数组内插到PHP中的单个逗号分隔列表中

时间:2015-12-23 15:20:09

标签: php mysql

如果我有一个类似的数组:

Array ( [0] => abcdef )
Array ( [0] => abcdef [1] => abcdef )
Array ( [0] => abcdef [1] => abcdef [2] => abcdef )
Array ( [0] => abcdef [1] => qwerty ) 

想要创建逗号分隔列表,如:

abcdef, abcdef, abcdef, abcdef, abcdef, abcdef, abcdef, qwerty

我该怎么做?

当我尝试像这样破坏数组时:

while($row = mysql_fetch_array($search)){ 
    $row2 = $row['scholarshipid']; 
    $newarray = unserialize($row2);
    $unique_array = array_unique($newarray, SORT_REGULAR);
    $commaList = implode(', ', $unique_array);
    echo "$commaList";
    //print_r($newarray); 
} 

$commaList = implode(', ', $unique_array);

我明白了:

abcdefabcdefabcdefabcdef, qwerty

2 个答案:

答案 0 :(得分:2)

comments开始,结果表明您向我们展示的输出来自循环。因此,您首先必须将每次迭代中的所有数组收集到1个变量中,然后可以进行内爆,例如。

$result = [];

while($row = mysql_fetch_array($search)){
    $row2 = $row['scholarshipid'];
    $newarray = unserialize($row2);
    $unique_array = array_unique($newarray, SORT_REGULAR);
    $result = array_merge($result, $unique_array);
}

echo implode(",", $result);

答案 1 :(得分:-2)

你可以这样做 UPDATE

$temp = array();
while($row = mysql_fetch_array($search)){ 
    $row2 = $row['scholarshipid']; 
    $newarray = unserialize($row2);
    $temp = array_merge($temp, $newarray);

}
$commaList = implode(', ', $temp);
print_r($commaList);

并且全部完成了:P