假设我有一个如下所示的 mysql 表:
id value
1 01
2 03
3 02
4 15
5 05
6 04
7 06
8 10
9 07
10 09
11 08
12 11
13 12
14 14
15 13
16 16
如何将它们转换为这样的字符串:
01,03,02,15|05,04,06,10|07,09,08,11|12,14,13,16
任何帮助将不胜感激,谢谢!
最佳答案
$result = "SELECT value FROM `your_table` ORDER BY id ASC";
$i = 0;
$string = '';
while($row = mysql_fetch_assoc($result)){
$i++;
$string += $row['value'].',';
if($i % 4 == 0){
$string = substr_replace($string, '|',strlen($string) - 1, 1);
}
}
$string = substr($string, 0, strlen($string) - 1);
关于php - 使用php将mysql表数据转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8785443/