Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-语法[数组/变量]_Php_Arrays_Variables - Fatal编程技术网

PHP-语法[数组/变量]

PHP-语法[数组/变量],php,arrays,variables,Php,Arrays,Variables,我想使用stripos()来过滤变量$background,如果找到的字符返回键值为$background if(stripos($background,$color)!==false){$background=$color中键的值}否则{$background='No color Found'} $color = array ( 'R' => "Red", 'Y' => "Yellow", '$bgcolor' => ar

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

if(stripos($background,$color)!==false){$background=$color中键的值}否则{$background='No color Found'}

$color = array (
         'R' => "Red",
         'Y' => "Yellow",
         '$bgcolor' => array (
                       '#ffffd0' => "Yellow",
                       '#ddffdd' => "Green", ));
变量$bgcolor先前已定义,并返回十六进制颜色代码。
上述语法正确吗?

对于
变量键
使用
双引号
。带有
单引号的字符串将忽略变量

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

$x = 'check';

$str = "$x";

echo $str, PHP_EOL;

$str = '$x';

echo $str, PHP_EOL;
?>

对于您提供的代码的其余部分,我想您需要如下内容:

<?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;
?>

谢谢行。。对“$bgcolor”(一个变量)使用双引号将起作用?else的if条件在哪里。可能会有错误。
<?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