开发者

php: parsing and converting array structure

开发者 https://www.devze.com 2023-02-25 10:16 出处:网络
I need to convert one array structure into another array structure. I hope someone will find it worthy their time to show how this could be done in a simple manner. It\'s a little above my array mani

I need to convert one array structure into another array structure.

I hope someone will find it worthy their time to show how this could be done in a simple manner. It's a little above my array manipulation skills.

The structure we start out with looks like this:

$cssoptions = array(

    array(
        'group'     => 'Measurements'
    ,   'selector'  => '#content'
    ,   'rule'      => 'width'
    ,   'value'     => '200px'
    ) // end data set

,   array(
        'group'     => 'Measurements'
    ,   'selector'  => '#content'
    ,   'rule'      => 'margin-right'
    ,   'value'     => '20px'
    ) // end data set

,   array(
        'group'     => 'Colors'
    ,   'selector'  => '#content'
    ,   'rule'      => 'color'
    ,   'value'     => '#444'
    ) // end data set

,   array(
        'group'     => 'Measurements'
    ,   'selector'  => '.sidebar'
    ,   'rule'      => 'margin-top'
    ,   'value'     => '10px'
    ) // end data set

); // END $cssoptions

It's a collection of discreet datasets, each consisting of an array that holds two key => value pairs describing a 'css-rule' and a 'css-rule-value'.

Further, each dataset holds a key => value pair describing the 'css-selector-group' that the 'css-rule' should blong to, and a key => value pair describing a 'rule-group' that should be used for structuring the rendering of the final css into a neat css code block arranged by the kind of properties they describe (colors, measurement, typography etc..)

Now, I need to parse that, and turn it into a new structure, where the:

    'rule'  => 'rule-name'
,   'value' => 'value-string'

for each dataset is converted into:

'rule-name' => 'value-string'

..and then placed into a new array structure where all 'rule-name' => 'value-string' pairs should be aggregated under the respective 'selector-values' Like this:

'#content' => array(

    'width' => '200px'
,   'margin-right' => '20px'            

) // end selecor block

..and finally all those blocks should be grouped under their respective 'style-groups', creating a final resulting array structure like this:

$css => array(

    'Measurements' => array(

        '#content' => array(

            'width' => '200px'
        ,   'margin-right' => '20px'            

        ) // end selecor block


    ,   '.sidebar' => array(

            'margin-top' => '10px'

        ) // end selector block

    ) // end rule group


,   'Colors' => array(

       开发者_JAVA百科 '#content' => array(

            'color' => '#444'

        ) // end selector block

    ) // end rule group

); // end css


It is not clean and tested code, but something like this should do the job:

$css = array();    
foreach($cssoptions as $cssoption)
{
    $css[$cssoption['group']][$cssoption['selector']][$cssoption['rule']] = $cssoption['value'];
}

Note: If you specify more values of the same group+selector+rule combination only the last will preserve.

0

精彩评论

暂无评论...
验证码 换一张
取 消