简体   繁体   中英

Convert array to comma separated values

I'm using PHP.

I have the following array:

Array
(
[home] => 9
[pets] => 8
[dogs] => 7
[shampoo] => 7
[cover] => 6
)

I want to create a comma separated list which is:

home,pets,dogs,shampoo,cover

Here's what I'm trying but giving me blank string ( $words is the array):

$myWords = implode(',',$words[0]);

Do I need to loop instead?

You're close. You just need the keys from that array. array_keys() will do that for you:

 $myWords = implode(',',array_keys($words));
$string = implode(',', array_keys($words));

$words[0]在您的数组中不存在,因为您的所有键都是字符串。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM