无法访问多维关联数组

时间:2013-08-02 05:11:19

标签: php arrays

我有以下代码:

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

编辑echo 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 ) )

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

由于您的$cards1是一个数组,并且您要将此数组分配给$_SESSION['value'][],因此您希望使用以下代码访问cardValue

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

答案 2 :(得分:0)

使用此

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

数组值在索引0