php: parsing and converting array structure

Posted by mwb on Stack Overflow See other posts from Stack Overflow or by mwb
Published on 2011-04-13T11:53:39Z Indexed on 2012/06/11 22:40 UTC
Read the original article Hit count: 156

Filed under:
|

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(

        '#content' => array(

            'color' => '#444'

        ) // end selector block

    ) // end rule group

); // end css

© Stack Overflow or respective owner

Related posts about php

Related posts about arrays