On Sep 17, 2008, at 10:54 AM, Nathan Rixham wrote:
Eric Gorr wrote:
On Sep 17, 2008, at 8:54 AM, Hunt Jon wrote:
Hi, I'm new to PHP. I have an array that I would like to convert
into a string.
For example, I have
array(
0 => "Good morning",
1 => "Good afternoon",
2 => "Good evening",
3 => "Good night"
);
Now I would like to convert the array to something like:
"Good morning Good afternoon Good evening Good night".
The result has concatanation of each value, but has one space
between it.
If possible, I would like to insert something like "\n" instead of
space.
I've been looking into various "array" functions, but I have no clue
where to start.
Of course, implode is the best option, but this can be accomplished
in other ways as well. Since you were unaware of basic array
processing and string techniques, I thought I would mention that it
can be done with a 'for' loop...
$asString = "";
$nItems = count( $myArray );
for ( $x = 0; $x < $nItems - 1; $x++ ) {
$asString .= $myArray[ $x ];
$asString .= " ";
}
$asString .= $myArray[ $nItems - 1 ];
there's foreach too..
$asString = '';
foreach( $myArray as $index => $value ) {
$asString .= $value . ' ';
}
BUT implode is the way to do it in this scenario :)
That foreach doesn't quite work as requested...there will be an extra
space at the end of the string.
Of course, processing the $asString with rtrim will get rid of it.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php