doutuosai3504
2014-11-04 19:18
浏览 64

从php数组中删除逗号

I'm currently working on displaying some arrays using PHP and MySQL.

Simplified code looks like this:

$sql = "SELECT * FROM workorder";
$result = mysqli_query($dbc3,$sql);
$tryout_reports = array();
while ( $row = mysqli_fetch_assoc($result) ) {
   $tryout_reports[] = $row['file_name'] . "<br />";
}
$detail_report['tryout_reports'] = $tryout_reports;

I took a lot out of the SQL here, so if there's something wrong in there please don't mind. The SQL statement works fine.

The problem is it's being outputted with commas separating each value, and it's looking real bad.

The HTML on the page when run looks something like this:

File1.pdf <br>
","
File2.pdf <br>
","
etc..

Now, I've looked around quite a bit and i've tried the following:

ltrim()
rtrim()
explode()
str_replace()

I know rtrim works, but it doesn't remove the commas. I tried something like:

$tryout_reports[] = rtrim($row['file_name'],"pdf");

And it took the pdf out of the end of each one, but when I replace "pdf" with "," it does nothing.

Feeling really stuck, any help would be really appreciated!

Thanks

EDIT: Forgot to add, the page that uses this data and displays it on the page has this:

table.append("<tr><th>Tryout Reports </th><td>" + data[0].tryout_reports + "</td></tr>");

That is inside of a jQuery function which runs when a job number is clicked to display additional information.

图片转代码服务由CSDN问答提供 功能建议

我目前正在使用PHP和MySQL显示一些数组。

简化代码如下所示:

  $ sql =“SELECT * FROM workorder”; 
 $ result = mysqli_query($ dbc3,$ sql); 
 $ tryout_reports =  array(); 
while($ row = mysqli_fetch_assoc($ result)){
 $ tryout_reports [] = $ row ['file_name']。  “&lt; br /&gt;”; 
} 
 $ detail_report ['tryout_reports'] = $ tryout_reports; 
   
 
 

我从SQL中花了很多钱 在这里,所以如果那里有什么问题请不要介意。 SQL语句工作正常。

问题是输出时用逗号分隔每个值,看起来真的很糟糕。

上面的HTML 运行时的页面如下所示:

  File1.pdf&lt; br&gt; 
“,”
 
File2.pdf&lt; br&gt; 
“,”
etc。  。
   
 
 

现在,我已经看了很多,我尝试了以下内容:

   ltrim()
rtrim()
explode()
str_replace()
   
 
 

我知道rtrim有效,但它不会删除逗号。 我试过类似的东西:

  $ tryout_reports [] = rtrim($ row ['file_name'],“pdf”); 
   \  n 
 

并且每个人都把PDF格式化了,但是当我用“,”替换“pdf”时,它什么也没做。

感觉真的卡住了,任何 非常感谢帮助!

谢谢

编辑:忘记添加,使用此数据并在页面上显示的页面包含:

  table.append(“&lt; tr&gt;&lt; th&gt; Tryout Reports&lt; / th&gt;&lt; td&gt;”+ data [0] .tryout_reports +“&lt;  / td&gt;&lt; / tr&gt;“); 
   
 
 

这是在单击作业编号以显示其他信息时运行的jQuery函数内部。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douxie4583 2014-11-05 20:08
    已采纳

    That comma comes from javascript because you parse directly the array. You can use join: w3schools.com/jsref/jsref_join.asp

    点赞 评论

相关推荐 更多相似问题