0

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...

1
  • 1
    where code of your loop is? Commented Mar 5, 2013 at 10:44

2 Answers 2

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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;
    }
}
?>

Comments

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.