PHP - 语法[array / variable]

时间:2013-04-18 08:44:00

标签: php arrays variables

我想使用stripos()过滤变量$background,如果找到的字符将键值返回为$background;

if(stripos($background, $color) !== false) { $background = the value of keys in $color } else { $Background = 'No Color Found'};

$color = array (
         'R' => "Red",
         'Y' => "Yellow",
         '$bgcolor' => array (
                       '#ffffd0' => "Yellow",
                       '#ddffdd' => "Green", ));

变量$ bgcolor先前已定义并返回十六进制颜色代码。 上面的语法是否正确?

1 个答案:

答案 0 :(得分:0)

double quotes使用variable keysingle quotes的字符串忽略变量。

<?php
header('Content-Type: text/plain;');

$x = 'check';

$str = "$x";

echo $str, PHP_EOL;

$str = '$x';

echo $str, PHP_EOL;
?>

节目:

check
$x

对于你提供的代码的其余部分,我猜你想要这样的东西:

<?php
//header('Content-Type: text/plain;');

$background = '#ffffd0';

$color = array (
        'R' => "Red",
        'Y' => "Yellow",
        '#ffffd0' => "Yellow",
        '#ddffdd' => "Green"
    );

$result = array_key_exists($background, $color) ? $color[$background] : 'not set' ;

echo $result;
?>

节目:

Yellow