用键和值内爆这个关联数组的最佳方法是什么


What would be the best way to implode this associative array with keys and values

我有一个盒子模型阵列

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);

所以我最终错过了第二个数组索引。

获得所需结果的最快方法是什么?感谢您的帮助。

试试这个:

$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);