php中的foreach删除密钥[关闭]

I use this code for the loop:

<?php foreach($_SESSION['accessories'] as $accessory) : ?>

    <?php var_dump($accessory); ?>

<?php endforeach; ?>

The problem is that now I can access the content of each accessory but without knowing the id of that accessory, because the original array look like this:

[accessories] => Array
    (
        [63] => Array
            (
                [price] => 15000
                [name] => Product 2
            )

        .
        ..
        ...

    )

And the 63 is lost ;(

Any idea how to get the 63 too?

<?php foreach($_SESSION['accessories'] as $key => $accessory) : 

    echo $key;                 // prints id 63
    echo $accessory['price'];  // prints price
    echo $accessory['name'];   // prints names 

   endforeach; ?>

You need to add $key to your foreach loop

<?php foreach($_SESSION['accessories'] as $key => $accessory) : ?> 
<?php var_dump($accessory); ?> 
<?php endforeach; ?>

This will give key 63 which you are looking for..