2011-07-13 59 views
2

嗨,這可能是非常基本的,但不能爲我的生活做。將PHP的foreach放入單個變量逗號分隔字符串

我有一個foreach輸出值。

$tags = get_the_tags(); 

foreach($tags as $v) { 

    echo $v; 
} 

這將輸出

php,css,cms,seo 

我需要把所有到一個單一的變量,所以我可以做到以下幾點。

$keywords = "<meta name='keywords' content='".$tags."'>"; 
return $keywords; 

有人可以幫助我試圖

$keywords = "<meta name='keywords' content='".foreach($tags as $v) {echo $v;}."'>"; 
    return $keywords; 

明顯得到錯誤從這個和它的錯誤它怎麼辦???

感謝

回答

12

使用破滅():

$tmp = ''; 
foreach($tags as $v) { 
    $tmp .= $v; 
} 

$keywords = "<meta name='keywords' content='".$tmp."'>"; 
+1

這正是我一直在感謝之後.... – DCHP

+0

感謝GRT答案 –

1

你不需要的foreach在:

$keywords = "<meta name='keywords' content='".implode(',', $tags)."'>"; 

您還可以,如果你使用的foreach建立一個臨時變量達到同樣的效果這種情況下implode是你在找什麼

$tags=implode(',',$tags); 

$keywords = "<meta name='keywords' content='".$tags."'>"; 
    return $keywords; 
0

試試這個

$tags=implode(',',$tags); 
0
foreach ($order->line_items as $chinak){ 

    $promotion_value=array(); 
    $commission_value=array(); 
    $addition_value=array(); 
     foreach($chinak->commission_one as $key_pro_com_addition=>$pro_com_value){ 


      $promotion=data_retrieve('promotion_setup', 'promotion_code', $key_pro_com_addition); 
      $addition=data_retrieve('value_addition_setup', 'value_code', $key_pro_com_addition); 
      $commission=data_retrieve('commission_setup', 'commission_code', $key_pro_com_addition); 


      if($promotion['promotion_code']!=NULL){ 
       $promotion_value[]= $promotion['promotion_code'].'-'.$pro_com_value; 
      } elseif($addition['value_code']!=NULL){ 
       $addition_value[]= $addition['value_code'].'-'.$pro_com_value; 
      }elseif($commission['commission_code']!=NULL){ 

       $commission_value[]= $commission['commission_code'].'-'.$pro_com_value; 

      } 


    } 

    $promotion_comma=implode(',',$promotion_value); 
    $addition_comma=implode(',',$addition_value); 
    $commission_comma=implode(',',$commission_value); 
      $sql_com_prom="INSERT INTO ".TB_PREF."pro_com_value_addition(order_no,stock_id,value_addition,promotion,commission) VALUES(";$sql_com_prom.=$order_no.",".db_escape($chinak->stock_id).",".db_escape($addition_comma).",".db_escape($promotion_comma).",".db_escape($commission_comma).")"; 

       db_query($sql_com_prom); 

    }