Forum Moderators: coopster
If I do
<?php
$array = array('Me', 'You', 'She');
$new_array=implode(',', $array);
print_r($new_array); // output 'Me,You,She'
?>
My qusetion is:
How can I use the implode function in this case to output 'Me','You','She'?
I tried doing print_r('","', $new_array); but there was no opening quote for the first value and no closing quote for the last one.
$array = array('Me', 'You', 'She');
$output = "'"; // add starting '
$length = count($array)-1; // as we need the length of the keys, not the counted length
$i = 0;
while ($i <= $length) { // for all elements other than the last add a , after the value
$output.= "$array[$i], ";
$i++
}
if ($i == $length) { // for the last element close the '
$output.= "$array[$i]'";
}
echo $output;
<edit>
Durr...maybe I should redo that to get what you wanted.
// answer == 'Me','You','She'
$array = array('Me', 'You', 'She');
$output = '';
$length = count($array)-1;
$i = 0;
while ($i <= $length) {
$output.= "'$array[$i]', ";
$i++
}
if ($i == $length) {
$output.= "'$array[$i]'";
}
echo $output;
[edited by: PHP_Chimp at 6:55 pm (utc) on April 8, 2008]