0

我正在尝试替换下面的代码

$Palette = array(
                 "0"=>array("R"=>188,"G"=>224,"B"=>46,"Alpha"=>100),
                 "1"=>array("R"=>188,"G"=>224,"B"=>46,"Alpha"=>100),
                 "2"=>array("R"=>188,"G"=>224,"B"=>46,"Alpha"=>100),
                 "3"=>array("R"=>188,"G"=>224,"B"=>46,"Alpha"=>100),
                 "4"=>array("R"=>176,"G"=>46,"B"=>224,"Alpha"=>100),
                 );

R的值相似但不同。到目前为止,我已经编写了以下代码作为替代:GB

$x = '0';
$incrementarray = array("0"=>"A","1"=>"B","2"=>"C","3"=>"D","4"=>"E");

do
{
    if ($correct == $incrementarray[$x])
    {
        $colour[$x] = '"'.$x.'"=>array("R"=>188,"G"=>224,"B"=>46,"Alpha"=>100),';
    }
    else 
    {
    $colour[$x] =  '"'.$x.'"=>array("R"=>255,"G"=>51,"B"=>51,"Alpha"=>100),';
    }
    $x++;
}
while ($x <= '4');

$allcolours = $colour[0].$colour[1].$colour[2].$colour[3].$colour[4];

但是,当我使用下面的行将它实现到我的脚本中时,它不起作用。

$Palette = array($allcolours);
4

2 回答 2

0
$x = '0';
$incrementarray = array("0"=>"A","1"=>"B","2"=>"C","3"=>"D","4"=>"E");

do
{
    if ($correct == $incrementarray[$x])
    {
        $Pallete[$x] = array("R"=>188,"G"=>224,"B"=>46,"Alpha"=>100);
    }
    else 
    {
    $Pallete[$x] =  array("R"=>255,"G"=>51,"B"=>51,"Alpha"=>100);
    }
    $x++;
}
while ($x <= '4');
于 2013-04-14T17:35:23.927 回答
0

$x 有一点过度使用。
事实上,你根本不需要那个变量

$Palette = array();
$incrementarray = array("0"=>"A","1"=>"B","2"=>"C","3"=>"D","4"=>"E");
foreach ($incrementarray as $value)
{
    if ($correct == $value)
    {
        $Palette[] = array("R"=>188,"G"=>224,"B"=>46,"Alpha"=>100);
    }
    else 
    {
        $Palette[] = array("R"=>255,"G"=>51,"B"=>51,"Alpha"=>100);
    }
}

您需要创建数组,而不是创建数组的 PHP 代码。

于 2013-04-14T17:36:24.297 回答