将值存储在以逗号分隔的变量中(,)

时间:2016-09-30 10:49:36

标签: php codeigniter

我有一个foreach循环。哪个有变量。如何将变量值存储在以逗号分隔的单个变量中,

foreach($variable as $key => $value){
   $variable = $value->Id;
}

2 个答案:

答案 0 :(得分:3)

请勿使用已使用的变量名称。

$otherV = '';
foreach($variable as $key => $value){
   if ($otherV) $otherV .= ',';
   $otherV .= $value->Id;
}

答案 1 :(得分:-1)

使用php implode()功能:

$str = implode(',',$array);

请参阅php.net http://php.net/manual/en/function.implode.php

如果您的$variable是对象,请先将其转换为get_object_vars()

的数组
相关问题