Reputation: 11
I'm trying to use arrays to return a specific CSS code, but I haven't found an automatic way to make the value of the array change according to the array. Here's what i'm trying to do:
$css = array(
"bgcolor" => array(
"wh" => array(* => "background-color:rgba(255,255,255,*);"),
"bk" => array(* => "background-color:rgba(0,0,0,*);")
),
"textcolor" => array(
"wh" => "color: #FFFFFF;",
"bk" => "color: #000000;"
),
"fade" => array(
"wh" => array(* => "box-shadow:#FFF 0px 0px *px;"),
"bk" => array(* => "box-shadow:#000 0px 0px *px;")
)
);
I'm using the * just to illustrate what i'm trying to achieve. This is what the code is supposed to return:
$css["bgcolor"]["wh"][0.5]; // print background-color:rgba(255,255,255,0.5);
$css["textcolor"]["wh"]; // print color: #FFFFFF;
$css["fade"]["wh"][100]; // print box-shadow:#FFF 0px 0px 100px;
I don't know if what I want is possible, but I think it is, I just dont know how. :D
Upvotes: 1
Views: 232
Reputation: 1671
If you are using PHP 5.3 or greater, you can achieve this by using anonymous functions like so:
<?php
$css = array(
'bgcolor' => array(
'wh' => function($s) {
return 'background-color: rgba(255, 255, 255, ' . $s . ');';
},
'bk' => function($s) {
return 'background-color: rgba(0, 0, 0, ' . $s . ');';
}
)
);
echo($css['bgcolor']['bk'](0.25)); // Returns rgba(0, 0, 0, 0.25)
echo(PHP_EOL);
echo($css['bgcolor']['wh'](0.75)); // Returns rgba(255, 255, 255, 0.75)
?>
I hope this is what you were after.
Upvotes: 2
Reputation: 13812
Using a function
<?php
echo getStyle("bgcolor", "wh", 0.5);
echo getStyle("textcolor", "wh", 0.5);
echo getStyle("fade", "wh", 100);
function getStyle($style, $color, $px = 0) {
$css = array(
"bgcolor" => array(
"wh" => "background-color:rgba(255,255,255,*px);",
"bk" => "background-color:rgba(0,0,0,*px);"
),
"textcolor" => array(
"wh" => "color: #FFFFFF;",
"bk" => "color: #000000;"
),
"fade" => array(
"wh" => "box-shadow:#FFF 0px 0px *px;",
"bk" => "box-shadow:#000 0px 0px *px;"
)
);
return str_replace("*", $px, $css[$style][$color]);
}
Upvotes: 0