1

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

1
  • What you are trying to achieve is use arrays to generate values. That is not possible. azcn2503's reply below is probably your best bet. Commented Jul 4, 2013 at 19:34

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it worked! Only a minor correction: Where is return 'background-color: rgba(255, 255, 255, ' . $s . ')'; should be return 'background-color: rgba(255, 255, 255, ' . $s . ');'; Thanks mate
Oops, I missed the semicolon! Thanks for your feedback.
0

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]);
}

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.