Stranger
Stranger

Reputation: 10610

Array of styles into regular CSS

I'm having a array which contains all the styles and i have to change them into proper CSS by looping in PHP.

For eg.,

Array
(
    [#outlook a] => Array
        (
            [padding] => Array
                (
                    [0] => 0
                )

        )

    [body] => Array
        (
            [width] => Array
                (
                    [0] => 100% !important
                )

            [-webkit-text-size-adjust] => Array
                (
                    [0] => none
                )

            [margin] => Array
                (
                    [0] => 0
                )

            [padding] => Array
                (
                    [0] => 0
                )

            [background-color] => Array
                (
                    [0] => #FAFAFA
                )

        )
[.preheaderContent div a] => Array
    (
        [color] => Array
            (
                [0] => #336699
            )

        [font-weight] => Array
            (
                [0] => normal
            )

        [text-decoration] => Array
            (
                [0] => underline
            )

    )

[.preheaderContent div a:visited] => Array
    (
        [color] => Array
            (
                [0] => #336699
            )

        [font-weight] => Array
            (
                [0] => normal
            )

        [text-decoration] => Array
            (
                [0] => underline
            )

    )

)

Like this i'll have a big array which contains all the style information. So i have to convert this into proper CSS. I know this is very simple. But i am failing somewhere on the recursive loop. This is the function i'm usig for this.

function cssbuild(&$cssArray)
{
    foreach ($cssArray as $selector => $style) 
    {
        if(is_array($style))
        {
            return $this->cssbuild($style);
        }
        else
        {
            $cssArray[$selector] = $cssArray[$selector].":".$style.";";
        }
    }
}

Any idea of how to do this would be greatly appreciated...

Upvotes: 0

Views: 78

Answers (2)

Kabir
Kabir

Reputation: 2156

Try This:

function createCSS($cssArray)
{
    $css = '';
    foreach ($cssArray as $classname => $properties)
    {
        $css .= "$classname{";
        foreach( (array) $properties as $propertyname => $propertyvalue )
        $css .= $propertyname .": ".$propertyvalue[0].";";
        $css .= "}";
    }
    return $css;
}

Upvotes: 2

eL-Prova
eL-Prova

Reputation: 1094

You have no recursion in the given example...

You can do something with this for recursion

<?php
BuildList($arrayParent, 0);

function BuildList($arrayElements, $depth)
{
    foreach($arrayElements as $element => $style)
    {
        echo str_repeat("&nbsp;&nbsp;", $depth) . $element . ":" . $style;
        $depth++;
        if(count($element->subCSSElement) > 0)
            $depth = BuildList($element->subCSSElement, $depth);

        return $depth;
    }
}
?>

Upvotes: 0

Related Questions