doujiu8479 2012-07-19 10:15
浏览 287
已采纳

如何将结果集数组拆分为php中的字符串

i need help. i was developed a page in smarty , i got a result set from a query and i need to change the result set to string and stored in text area

my query is given below

select val from test

my result set is print in var_dump in controller

{ [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" } }

i need to change in to sting like c,c++,java,PHP

the changing function is preformed only controller

ple help me.. and thk adv

  • 写回答

3条回答 默认 最新

  • doupo6967 2012-07-19 10:18
    关注

    Use foreach for that. See more information here - http://php.net/manual/en/control-structures.foreach.php .

    Example -

    $array = Array("333", "222", "111");
    
    foreach($array as $string) {
        echo $string.'<br />';
    }
    

    Another solution would be to use implode.

    See more information here - http://php.net/manual/en/function.implode.php and again a small example -

        $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
    

    EDIT:

    For writing in file you must use file functions.

    Check this link - http://php.net/manual/en/function.file-put-contents.php

    example -

        // 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);
    

    Suggestion:

    When you are writing SQL queries, I would suggest that you already now start learning to write them correctly, so they are easier to read.

    For example, your query -

    select val from test
    

    Could be changed to -

    SELECT `val` FROM `test`
    

    which is alot easier to read and understand.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器