2015-09-04 260 views
0

有沒有辦法將值賦給關聯數組而不用數組創建另一個維? $ champion ['Champion']] ['New'] = $ row ['New']; $ {$ row ['Champion']] ['New'] = $ row ['New'];將值賦給多維關聯數組

陣列這樣

enter image description here

'新' 我怎麼能訪問這個價值?

$champion=array(); 

        $champions_table= $conn->prepare("SELECT Champion, New, Passive, Q, Q2, W, W2, E, E2, R, R2 FROM champions Where Patch_No = ? ORDER BY ID ASC"); 
        $champions_table->bind_param('s', $Patch_No); 
        $champions_table->execute(); 
        $champions_table_result = $champions_table->get_result(); 

while($row = $champions_table_result->fetch_assoc()){ 
         //Getting General Changes 
         $champion[$row['Champion']]['New'] = $row['New']; 
} 
+0

道歉,如果我的誤解,但你可以用$ existi爲數組賦值ngarray ['new'] = $ whateveryouwant' – mattslone

+0

@mattslone如何檢索所述值?我已經嘗試過像echo $ champions [$ row ['Champion']] ['New'];沒有成功 – Higeath

+0

不,我只是想知道如果'新'價值是0或1爲specyfic冠軍回聲$冠軍['東西'] ['新'],我想從這個1或0 – Higeath

回答

1

獲取所有賦值。

foreach ($champion as $key => $value) { 
    echo "Champion : ".$key." Value: ".$value['New']." <br />\n"; 
} 

,如果你知道冠軍的名字

echo $champion['championname']['New'] <br />\n"; 

例如

echo $champion['Blitzcrank']['New'] <br />\n"; 

如果你想有一個3維數組略微擴大到更容易地看到它是如何分配給

while($row = $champions_table_result->fetch_assoc()){ 
     $value = $row['Champion']; 
     $champion['Champion'][$value]= array('New' => $row['New']); 
} 

獲得值

echo $champion['Champion']['Blitzcrank']['New'] <br />\n";  
+0

echo $冠軍['冠軍'] ['新']我已經嘗試過,我知道冠軍的名字,我得到身份不明的索引 – Higeath

+0

@Higeath請看看我的更新。 –

+0

@Higeath:你能給我一個迴應嗎? –

0

假設這是從數據庫結果:

$row = array('Champion' => 'champ', 'New' => '1');

這應該工作:

$champion[$row['Champion']]['New'] = $row['New']; 
echo $champion[$row['Champion']]['New']; 

而且應該輸出1

+0

使用'$ champion'的OP [$ row ['Champion']] ['New'] = $ row ['New'];'so'$ champion ['?']'成爲'$ row ['Champion ']'!! –

+0

對..我的答案沒有反映出來嗎?我只是迴應來自數據庫的當前記錄。 – mattslone

+0

這隻會在循環中起作用('$ row'在循環內部有效)OP需要循環後的值,例如'echo $ champion ['Blitzcrank'] ['New'];'你的例子將像'echo $ champion ['champ'] ['New'];'這就和我的答案一樣。 –