2013-04-14 140 views
0

我試圖與該RGB類似的東西,但不同的值替換下面存儲陣列中的一個變量

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

的代碼。我已經寫了下面的代碼,只要更換:

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

回答

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'); 
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代碼來創建數組變量。