Reputation: 767
I need help in constructing a string concatenated using comma based on the key values of an array. It could be achieved using foreach
loop, but is there any method similar to using implode()
?
This is the sample array I use,
array(5) {
[2280]=> string(1) "1"
[2138]=> string(1) "1"
[3194]=> string(1) "1"
[2396]=> string(1) "1"
[2944]=> string(1) "1"
}
Thanks in advance !!!
Upvotes: 1
Views: 64
Reputation: 15080
Can be done like:
echo implode(', ', array_keys(array(2280 => '1', 2138 => '1')));
Output:
2280, 2138
Upvotes: 1
Reputation: 8893
say your array is named $array
, you can do it like that:
implode(',',array_keys($array))
array_keys will return the keys of your array in another array, which will then be used to implode the keys into the string you want.
that would generate the following string:
"2280,2138,3194,2396,2944"
Upvotes: 4