2013-08-02 199 views
0

我有以下代碼:無法訪問多維關聯數組

foreach ($cardSuits as $cardSuit) { 
    $keyCardValues = array_keys($cardValues); 
    foreach ($keyCardValues as $cardValue) { 
     $deck[] = array("cardValue" => $cardValue, "cardSuit" => $cardSuit); 
     shuffle($deck); 
    } 
} 

if ($deal == "Deal") { 
shuffle($deck); 
$cards1 = array_shift($deck); 
$_SESSION['value'][] = $cards1; 

我已經試過:

echo "<br />" . $_SESSION['value']['cardValue']; 

但它給我一個未定義的索引錯誤。但是,如果我做一個print_r,它工作正常.. 我如何回顯它,所以會話可以給我$ cardValue數組中?

感謝

編輯爲print_r的:

Array ([value] => Array ([0] => Array ([cardValue] => nine [cardSuit] => hearts)) [cards] => Array ([0] => 9 [1] => 2 [2] => 10 [3] => 4 [4] => 3 [5] => 10 [6] => 5 [7] => 2 [8] => 10 [9] => 5)) 

編輯回聲的print_r:

Array ([value] => Array ([0] => Array ([cardValue] => nine [cardSuit] => diamonds)) [cards] => Array ([0] => 9 [1] => 3 [2] => 7 [3] => 10 [4] => 9 [5] => 11 [6] => 7 [7] => 10 [8] => 10 [9] => 5)) 
+0

內你可以發佈'echo print_r($ _ SESSION)的結果;' –

+0

$ _SESSION ['value']是數組。使用foreach –

+0

添加print_r謝謝 – StevenWin

回答

0

因爲y我們$cards1是一個數組,你要分配這個數組$_SESSION['value'][],你要使用下面的訪問cardValue

echo "<br />" . $_SESSION['value'][0]['cardValue']; 
+0

太棒了!謝謝 – StevenWin

+0

@ yay4programming,不客氣。 – vee

1

嘗試

echo "<br />" . $_SESSION['value'][0]['cardValue'];

+1

最早的完整答案。 @ yay4programming,你錯過了[0] – smcjones

+1

非常感謝! – StevenWin

0

使用此

echo "<br />" . $_SESSION['value'][0]['cardValue']; 

陣列值是索引0