Jump to content

Create string from Array?


ale1981

Recommended Posts

If I have an array similar to;

 

Array ( [0] => 0010 [1] => 0020 [2] => 0030 [3] => 0040 [4] => 0050 [5] => 0060 )

 

How can i convert that array to a string, so the string will be like;

 

"0010", "0020", "0030", "0040", "0050", "0060"

 

Notice the "," is missing from the last part of the array.

If the array has only one entry then it would be;

 

"0010"

 

Any help appreciated.

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/96536-create-string-from-array/
Share on other sites

What are you doing with the string? If you are writing this to a file in order to create an Excel readable CSV file, check out the fgetcsv() and fputcsv() functions. Otherwise, you'd need something like this:

<?php
$arr = array('0010', '0020', '0030', '0040', '0050', '0060');
foreach ($arr as $k => $v)
{
  $arr[$k] = '"{$v}"';
}

echo implode(',' $arr);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.