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
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);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.