如何将结果集数组拆分为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

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.

If You need to join all array with some delimeters, then use implode. Example:

$arr = array("hi", "peter!", "how", "are", "you");
echo implode(" ", $arr) . "?";

//output
// hi peter! how are you?

If you want a string separated by commas, you must use the implode function

string implode ( string $glue , array $pieces )

glue: Defaults to an empty string. This is not the preferred usage of implode() as glue would be the second parameter and thus, the bad prototype would be used. pieces:The array of strings to implode.

Returns a string containing a string representation of all the array elements in the same order, with the glue string between each element.

http://www.php.net/manual/en/function.implode.php

Example

$array = Array("333", "222", "111");
$string = explode(',', $array);

returns

"333,222,111"

if you want spaces:

$string = explode(' ', $array);

returns

"333 222 111"