将数组存储在变量中

时间:2013-04-14 17:31:30

标签: php arrays variables

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

$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),
                 );

RGB具有类似但不同的值。到目前为止,我已经编写了以下代码:

$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);

2 个答案:

答案 0 :(得分: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');

答案 1 :(得分: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代码。