2017-05-04 74 views
3

我正在使用多個數組操作處理一個項目。PHP - 如何將多個鍵值數組轉換爲| (Pipe)分隔字符串

我有一個變量叫$ product_attributes它包含下面的數組作爲值。

Array 
(
    [0] => Array 
     (
      [0] => Applications 
      [1] => Steel; PVC; Std. Wall 
     ) 

    [1] => Array 
     (
      [0] => Blade Exp. 
      [1] => 0.29 
     ) 

    [2] => Array 
     (
      [0] => Fits Model 
      [1] => 153 
     ) 
) 

現在我想將它轉換爲| (管)組成的字符串象下面這樣:

Applications=Steel; PVC; Std. Wall|Blade Exp.=0.29|Fits Model=153 

下面是什麼我曾嘗試:

$tags = implode('|',$product_attributes); 
echo "Output".$tags; 

但如下返回輸出:

OutputArray|Array|Array|Array|Array|Array 
+0

這或許有助於http://stackoverflow.com/questions/16710800/implode-data-from-a-multi-dimensional-array –

回答

4

該解決方案使用array_mapimplode功能:

$result = implode("|", array_map(function ($v) { 
    return $v[0] . "=" .$v[1]; 
}, $product_attributes)); 

print_r($result); 

輸出:

Applications=Steel; PVC; Std. Wall|Blade Exp.=0.29|Fits Model=153 
+0

你總是智囊團。感謝它的作品 –

+0

@ManthanDave,歡迎,很高興幫助 – RomanPerekhrest

相關問題