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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度