使用键和值来破坏这个关联数组的最佳方法是什么

时间:2015-02-24 17:54:36

标签: php arrays

我有一个盒子模型数组

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

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

获得理想结果的最快方法是什么? 任何帮助表示赞赏。

1 个答案:

答案 0 :(得分: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);