0

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

1 Answer 1

2

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.

3
  • Elegant, I need to get used to thing not being more complicated. I do not need to specify more values of the same group+selector+rule combination, so this will do the trick. Thanks a lot. Commented Apr 13, 2011 at 12:29
  • what do you mean it's not clean and tested, it looks super clean how can it me more concise? .. and it works :) Thats good enough for me. Commented Apr 13, 2011 at 12:46
  • I did not tested so it was not tested (it might contain some syntax error). And I think almost everything you write in PHP is a little dirty ;) Commented Apr 13, 2011 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.