Reputation: 1645
I have a key-pair array like this
$option['33']="Steak Doness";
$option['34']="Size";
$option['35']="Cooking Method";
I want to store the keys into a string like this
$key="33,34,35,";
I try to use foreach loop
$key="";
foreach($option as $key => $value) {
$key=$key.",";
}
echo $key;
However, my output is
35,
May i know which part went wrong?
Upvotes: 0
Views: 959
Reputation: 744
1st change your varible $key to $keys replace one line
$key=$key.",";
to
$keys .= $key . ",";
it will work 100%
Upvotes: 0
Reputation: 9583
You miss use the $key
in your script.
The problem is with the $key
which is in the foreach loop.... In each time your $key
variable updated with the loop... Try with difference variable in your script.
OR, simply use
echo $key = implode(",", array_keys($option));
Upvotes: 1