2015-02-24 65 views
0

我有一個盒子模型陣列什麼是內爆這個關聯數組鍵的最佳方式和值

Array 
(
    [padding] => Array 
     (
      [padding-top] => 0px 
      [padding-right] => 0px 
      [padding-bottom] => 0px 
      [padding-left] => 0px 
     ) 

    [margin] => Array 
     (
      [margin-top] => 0px 
      [margin-right] => 0px 
      [margin-bottom] => 0px 
      [margin-left] => 0px 
     ) 

    [border] => Array 
     (
      [border-size] => 0px 
      [border-style] => solid 
      [border-color] => #ff6600 
     ) 

) 

,我需要輸出以下

padding-top : 0px; 
padding-right: 0px; 
padding-bottom: 0px; 
padding-left: 0px; 

margin-top : 0px; 
margin-right: 0px; 
margin-bottom: 0px; 
margin-left: 0px; 

border-size : 0px; 
border-style: solid; 
border-color: #ff6600; 

我開始用這個

$box_model   = array(); 
foreach($box_model_array as $key => $value){ 


    $box_model[$key] = $key.':'.implode(';',$value).'';   

} 


return implode('',$box_model); 

所以我最終失去了第二個數組索引。

什麼是獲得理想結果的最快方法? 任何幫助表示讚賞。

回答

2

試試這個:

$box_model = array(); 
foreach ($box_model_array as $group => $styles) { 
    foreach ($styles as $name => $value) { 
     $box_model[] = "$name: $value;"; 
    } 
    // If you really need the space in between the groups. 
    $box_model[] = ""; 
} 
$box_model = implode("\n", $box_model); 
+0

謝謝,準確的輸出要求。 – Benn 2015-02-24 20:25:23

相關問題