如何将查询返回的值数组转换为逗号分隔值

时间:2020-12-22 00:20:29

I have a result set that is being returned using this code:

我有一个使用此代码返回的结果集:

while ($row = mysql_fetch_array( $result )) {

echo "ID ".$row['v2id'];

}

this returns ID 2ID 3ID 4ID 8

这将返回ID 2ID 3ID 4ID 8

how would i convert this to comma separated values and then store them in a variable?

我如何将其转换为逗号分隔值,然后将它们存储在变量中?

so if i echoed out the variable, the final output would should look like 2, 3, 4, 8

所以,如果我回显变量,最终输出应该看起来像2,3,4,8

5 个解决方案

#1


6  

store all the values in an array, then join them using ", " as the glue

将所有值存储在数组中,然后使用“,”作为粘合剂将它们连接起来

$values = array();

while ($row = mysql_fetch_array( $result )) {
    $values[] = $row['v2id'];
}

echo join(", ", $values);

#2


1  

Add all to values to an array, then implode it using ', ' as glue

将所有值添加到数组中,然后使用','作为粘合剂将其内爆

$result = array();
while ($row = mysql_fetch_array($result)) {
    $result[] = $row['v2id'];
}
echo implode(', ', $result);

#3


1  

$data = array(1,2,'Hello',3,4,'1,234.56');

$outstream = fopen("php://temp", 'r+');
fputcsv($outstream, $data, ',', '"');
rewind($outstream);
$csv = fgets($outstream);
fclose($outstream);

#4


1  

$ids = array();

while ($row = mysql_fetch_array( $result )) {
    $ids[] = (int)$row['v2id'];
}

echo implode(", ", $values);

1ID 2ID 3ID 4ID 8 can be converted to int by (int)$row['v2id'] so $ids will contain int only.

1ID 2ID 3ID 4ID 8可以通过(int)$ row ['v2id']转换为int,因此$ ids只包含int。

#5


0  

My way would be this

我的方式就是这样

$csv = '';
while ($row = mysql_fetch_array( $result )) {
   /* Note the '.=' - appends variable */
   $csv .= "ID ".$row['v2id'];
   $csv .= ','; // This is the bit I missed out
}
/* Remove the final trailing comma */
$csv = substr($csv, 0, -1);
echo $csv;

The gold star for spotting the 'deliberate' mistake goes to ssapkota

发现“故意”错误的金星来到ssapkota

#1


6  

store all the values in an array, then join them using ", " as the glue

将所有值存储在数组中,然后使用“,”作为粘合剂将它们连接起来

$values = array();

while ($row = mysql_fetch_array( $result )) {
    $values[] = $row['v2id'];
}

echo join(", ", $values);

#2


1  

Add all to values to an array, then implode it using ', ' as glue

将所有值添加到数组中,然后使用','作为粘合剂将其内爆

$result = array();
while ($row = mysql_fetch_array($result)) {
    $result[] = $row['v2id'];
}
echo implode(', ', $result);

#3


1  

$data = array(1,2,'Hello',3,4,'1,234.56');

$outstream = fopen("php://temp", 'r+');
fputcsv($outstream, $data, ',', '"');
rewind($outstream);
$csv = fgets($outstream);
fclose($outstream);

#4


1  

$ids = array();

while ($row = mysql_fetch_array( $result )) {
    $ids[] = (int)$row['v2id'];
}

echo implode(", ", $values);

1ID 2ID 3ID 4ID 8 can be converted to int by (int)$row['v2id'] so $ids will contain int only.

1ID 2ID 3ID 4ID 8可以通过(int)$ row ['v2id']转换为int,因此$ ids只包含int。

#5


0  

My way would be this

我的方式就是这样

$csv = '';
while ($row = mysql_fetch_array( $result )) {
   /* Note the '.=' - appends variable */
   $csv .= "ID ".$row['v2id'];
   $csv .= ','; // This is the bit I missed out
}
/* Remove the final trailing comma */
$csv = substr($csv, 0, -1);
echo $csv;

The gold star for spotting the 'deliberate' mistake goes to ssapkota

发现“故意”错误的金星来到ssapkota