Many aspects of the application can be automated, for example we can use some PHP code (such as using arrays) to generate style sheets. So how to achieve it? The following article will show you how to convert nested PHP arrays into CSS rules and display them in the form of strings. I hope it will be helpful to you.
Let’s take a look at how to convert nested associative arrays into CSS characters.
1. Write a function to convert an associative array into a CSS string
To convert an array into a CSS string in PHP (in SASS or LESS case using rules or simple variables) we will use the following function:
<?php /** * @param array $rules * CSS规则的数组形式为: * array('selector'=>array('property' => 'value')). * 还支持选择器 * 嵌套示例: * array('selector' => array('selector'=>array('property' => 'value'))). * * @return 字符串一个CSS规则字符串。它不包含在<style>标签中。 */ function css_array_to_css($rules, $indent = 0) { $css = ''; $prefix = str_repeat(' ', $indent); foreach ($rules as $key => $value) { if (is_array($value)) { $selector = $key; $properties = $value; $css .= $prefix . "$selector {\n"; $css .= $prefix . css_array_to_css($properties, $indent + 1); $css .= $prefix . "}\n"; } else { $property = $key; $css .= $prefix . "$property: $value;\n"; } } return $css; } //调用css_array_to_css()函数转换 //code ?>
Description: This function basically expects as first argument an array containing CSS rules or simple properties, which is not an array Each key => value; will be represented as key : value;, and if the value of the key is an array, the rule for css will be introduced.
2. Use the function
As mentioned in the above function description, it returns a CSS character from an array with specified rules string. This function works fine with pure CSS rules, media queries, SASS and LESS as long as the structure of the array is valid. For example:
● Convert to CSS:
Add the following code after the css_array_to_css() function:
$stylesheet = array( "body" => array( "margin" => "0", "font-size" => "1rem", "font-weight" => 400, "line-height" => 1.5, "color" => "#212529", "text-align" => "left", "background-color" => "#fff" ), ".form-control" => array( "display" => "block", "width" => "100%!important", "font-size" => "1em", "background-color" => "#fff", "border-radius" => ".25rem" ) ); echo(css_array_to_css($stylesheet));
The previous code snippet will output the following CSS Rules:
body { margin: 0; font-size: 1rem; font-weight: 400; line-height: 1.5; color: #212529; text-align: left; background-color: #fff; } .form-control { display: block; width: 100%!important; font-size: 1em; background-color: #fff; border-radius: .25rem; }
● Conversion to SASS/SCSS:
Due to the recursive implementation, it will be possible to nest multiple rules within a rule, which allows us to generate valid Syntax:
$sass = array( "nav" => array( "ul" => array( "margin" => 0, "padding" => 0, "list-style" => "none" ), "li" => array( "display" => "inline-block" ), "a" => array( "display" => "block", "padding" => "6px 12px", "text-decoration" => "none" ) ) ); echo css_array_to_css($sass);
The previous snippet will output the following SASS code:
nav { ul { margin: 0; padding: 0; list-style: none; } li { display: inline-block; } a { display: block; padding: 6px 12px; text-decoration: none; } }
● Convert to LESS:
Works the same as SASS , we can also use LESS to write complex rules:
$less = array( "@nice-blue" => "#5B83AD", "@light-blue" => "@nice-blue + #111", "#header" => array( "color" => "@light-blue" ), ".component" => array( "width" => "300px", "@media (min-width: 768px)" => array( "width" => "600px", "@media (min-resolution: 192dpi)" => array( "background-image" => "url(/img/retina2x.png)" ) ), "@media (min-width: 1280px)" => array( "width" => "800px" ) ) ); echo css_array_to_css($less);
The previous code segment will output the following LESS code:
@nice-blue: #5B83AD; @light-blue: @nice-blue + #111; #header { color: @light-blue; } .component { width: 300px; @media (min-width: 768px) { width: 600px; @media (min-resolution: 192dpi) { background-image: url(/img/retina2x.png); } } @media (min-width: 1280px) { width: 800px; } }
Related video tutorial recommendations: "PHP Tutorial"
The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !
The above is the detailed content of How to convert nested PHP arrays to CSS rules? (code example). For more information, please follow other related articles on the PHP Chinese website!