作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我需要帮助。我在 smarty 中开发了一个页面,我从查询中得到了一个结果集,我需要将结果集更改为字符串并存储在文本区域中
下面是我的查询
select val from test
我的结果集在 Controller 的 var_dump 中打印
{ [0]=> array(1) { ["val"]=> string(1) "c" } [1]=> array(1) { ["val"]=> string(3) "c++" } [2]=> array(1) { ["val"]=> string(4) "java" } [3]=> array(1) { ["val"]=> string(3) "PHP" } }
我需要改成像 c,c++,java,PHP
改变功能只在 Controller 上执行
请帮助我..和 thk adv
最佳答案
为此使用 foreach。在此处查看更多信息 - http://php.net/manual/en/control-structures.foreach.php .
例子-
$array = Array("333", "222", "111");
foreach($array as $string) {
echo $string.'<br />';
}
另一种解决方案是使用内爆。
在此处查看更多信息 - http://php.net/manual/en/function.implode.php又是一个小例子-
$array = Array("333", "222", "111");
$strings = implode(",", $array); // comma in first quotes are seperator, you can set it also to " " for a single space.
echo $strings; // In this case it will output 333,222,111 if you would set it to empty space then it would output 333 222 11
编辑:
要写入文件,您必须使用文件函数。
检查此链接 - http://php.net/manual/en/function.file-put-contents.php
例子-
// your file
$file = 'sample.txt';
$array = Array("333", "222", "111");
// Add all strings to $content.
foreach($array as $string) {
$content .= $string.'<br />';
}
// write everything in file
file_put_contents($file, $content);
建议:
当您编写 SQL 查询时,我建议您现在就开始学习正确编写它们,这样它们更容易阅读。
例如,您的查询-
select val from test
可以改成-
SELECT `val` FROM `test`
这样更容易阅读和理解。
关于php - 如何在 php 中将结果集数组拆分为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11558641/
我是一名优秀的程序员,十分优秀!