简体   繁体   English

使用PHP foreach构建样式表

[英]Using PHP foreach to build stylesheet

Okay - so I'm creating a dynamic CSS stylesheet, which I want to set up with an array. 好的 - 所以我正在创建一个动态的CSS样式表,我想用数组设置它。

First let me say that I'm not a PHP expert, but I somewhat know my way around it. 首先让我说我不是PHP专家,但我有点了解我的方法。

Here's my very basic array. 这是我非常基本的数组。

$visual_css = array (
    array(
        "selector"  => "body",
        "property"  => "background",
        "value"     => "#FFF",
        "property2" => "color",
        "value2"    => "#000",
        "type"      => "css"
    )
);

So we have a selector, and two properties with values. 所以我们有一个选择器,以及两个带值的属性。

I now want to create the stylesheet, but I'm running into problems due to my lack of PHP knowledge. 我现在想要创建样式表,但由于缺乏PHP知识,我遇到了问题。

foreach ($visual_css as $value) {
    switch ($value['type']) {
        case "css":

            // Open selector
            echo ( !empty($value['selector']) ? $value['selector'] . '{' : null );

            foreach ($value as $key => $item) {
                foreach ($value as $key2 => $item2) {
                    //Match only the id's against the key
                    if (preg_match('/^property/i', $key) && preg_match('/^value/i', $key2)) {
                        // First property
                        echo ( !empty($item) ? $item . ':' : null );
                            echo ( !empty($item2) ? $item2 . ';' : null );
                    }
                }

            }

            // Close selector
            echo ( !empty($value['selector']) ? '}' : null );

        break;
    }
}

Now I know this code isn't correct, as it's outputting the following in the stylesheet: 现在我知道这段代码不正确,因为它在样式表中输出以下内容:

body{background:#FFF;background:#000;color:#FFF;color:#000;}

This is the desired result: 这是期望的结果:

body{background:#FFF;color:#000;}

So basically, I want to be able to create an unlimited number of properties and values with an incrementing number after them, and have the code write it out. 所以基本上,我希望能够创建无限数量的属性和值,并在它们之后加上一个递增的数字,并让代码写出来。

Can anyone help me? 谁能帮我?

Thanks! 谢谢!

So my approach uses a multidimensional array instead of a flat array to nest properties of selectors in sub-arrays. 因此,我的方法使用多维数组而不是平面数组来嵌套子数组中选择器的属性。 This way your approach is much more concise by first looping through the first layer, and then going through the children of each parent and composing the CSS for each property / value pair. 这样,通过首先遍历第一层,然后遍历每个父节点的子节点并为每个属性/值对组成CSS,您的方法更简洁。 It also makes iteration a cinch as opposed to having to inspect the values of keys to locate which pair you're at. 它还使迭代变得简单,而不是必须检查键的值以找到您所在的对。 If you need a different output format, your iteration code can decide that, and it should be left out of the structure 如果您需要不同的输出格式,您的迭代代码可以决定,并且它应该被排除在结构之外

$visual_css = array (
    'body'  => array(
        'background'    => '#FFF',
        'color'         => '#000'
    )
);

$output = '';
foreach($visual_css as $k => $properties) {
    if(!count($properties))
        continue;

    $temporary_output = $k . ' {';

    $elements_added = 0;

    foreach($properties as $p => $v) {
        if(empty($v))
            continue;

        $elements_added++;

        $temporary_output .= $p . ': ' . $v . '; ';
    }

    $temporary_output .= "}\n";

    if($elements_added > 0)
        $output .= $temporary_output;
}

echo $output;
// body {background: #FFF; color: #000; }

In your example you are looping over the same array twice, in this piece of code: 在您的示例中,您在这段代码中循环遍历同一个数组:

foreach ($value as $key => $item) {
    foreach ($value as $key2 => $item2) {

If you structured your data in a more logical way, you could make a cleaner loop code, too. 如果您以更合理的方式构建数据,那么您也可以制作更清晰的循环代码。 I'd suggest you to structure the array with each property&value as a separate sub-array, for example: 我建议你用每个属性和值构建数组作为一个单独的子数组,例如:

$visual_css = array(
    array(
        'selector' => 'body',
        'properties' => array(
            'background' => '#fff',
            'color' => '#000'
        )
    )
    // ... etc ...
);

And then you can easily loop through it like this: 然后你可以像这样轻松地遍历它:

foreach ($visual_css as $selector)
{
    echo $selector['selector'].' { ';
    foreach ($selector['properties'] as $name => $value)
    {
        echo $name.': '.$value.'; ';
    }
    echo ' } ';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM