dougong5285 2014-12-31 10:38
浏览 38
已采纳

如何在php中将零索引逗号分隔值分解为单个字符串

I want to break comma seperated values which are retrieved from database and then put each individual in a string and print it in a for loop only. I am getting the values from database but i am not be able to break values. Below is the code what i have done right till now.Please help to solve my issue. Thanks in advance.

$query="select * from create_segment";
$result = mysql_query($query,$link) or die(mysql_error());
$number_of_segment=mysql_num_rows($result);
while($row=mysql_fetch_assoc($result))
{
    $segments[]=$row;
}

foreach($segments as $success) {
 $thePostIdArray = explode(', ', $success['subjects']);
 for($i=0; $i < count($thePostIdArray); $i++)
  {
     echo "string...".$strings=$thePostIdArray[$i];  
  }
} 

It output:

string...1,2,3,4,5,6,7,8,9,10
string...1,2,3,4,5,6,7,8

but i want something like this:

for(......)
{
echo "values...".$i;
}

which should output

values...1
values...2
values...3 and so on.

my database structure is like:

id    subjects

1     1,2,3,4,5
2     1,2,7
  • 写回答

4条回答 默认 最新

  • dongzhen4180 2014-12-31 10:42
    关注

    Please try to use ',' instead of ', ' in your

    explode(', ', $success['subjects']);
    

    statement so it would look like:

    explode(',', $success['subjects']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?