在Symfony for PHP中使用foreach循环和会话显示结果

在Symfony for PHP中使用foreach循环和会话显示结果,php,symfony,session,foreach,shopping-cart,Php,Symfony,Session,Foreach,Shopping Cart,我正在使用SymfonyforPHP中的会话为鞋店网站编写一个购物车页面。它允许将选定大小的项目添加到购物车并计算数量。我需要知道如何使用foreach循环显示购物车 以下是添加产品的功能: public function add($shoesid, $sizeid, SessionInterface $session) { $cart = $session->get('cart', []); if(!empty($cart[$shoesid][$sizeid])) {

我正在使用SymfonyforPHP中的会话为鞋店网站编写一个购物车页面。它允许将选定大小的项目添加到购物车并计算数量。我需要知道如何使用foreach循环显示购物车

以下是添加产品的功能:

public function add($shoesid, $sizeid, SessionInterface $session)
{
    $cart = $session->get('cart', []);
    if(!empty($cart[$shoesid][$sizeid])) {
        $cart[$shoesid][$sizeid]++;
    } else {
        $cart[$shoesid][$sizeid] = 1;
    }
    $session->set('cart', $cart);
    dd($session->get('cart'));
}
以下是我添加项目时收到的消息:

> array:2 [▼   
40024 => array:1 [▼   
410 => 2     
]   
]
(其中40024为shoesid,410为sizeid,2为数量(计数为[shoesid][sizeid])

这里有一个显示购物车的功能

public function index(SessionInterface $session, ShoesRepository $shoesRepository, CountriesRepository $countriesRepository, StockRepository $stockRepository)
{
    $cart = $session->get('cart', []);
    $cartWithData = [];
    foreach($cart as $shoesid => [$sizeid => $quantity]) //error here
    {
        $cartWithData[] = [
            'shoe' => $shoesRepository->shoeDescr($shoesid),
            'shoePrice' => $shoesRepository->find($shoesid),
            'quantity' => $quantity,
            'size' => $stockRepository->shoeSizeCart($shoesid, $sizeid)
        ];
    }        
    $total=0;
    $delivery=20;

    foreach($cartWithData as $item) 
    {
        $totalItem = $item['shoePrice']->getShoesPrice()*$item['quantity'];
        $total += $totalItem;            
    }
    $countries = $countriesRepository->findBy(array(), array('countryname' => 'asc'));
    dd($cartWithData);
}
但是,当我尝试启动购物车页面时,会出现下一个错误(在第9行):

注意:未定义变量:sizeid


我应该对代码进行哪些更改才能正确显示项目该
$cart
似乎是一个多维数组(确切地说是两个),具有以下结构:

$cart = [
    <$shoeId> => [
        <$sizeId> => <count>,
        <$sizeId> => <count>,
        ...,
    ],
    <$shoeId> => [
        ...
    ],
];
// Iterate on the 1st lelvel: shoes
foreach ($cart as $shoeId => $sizes) {
    // Iterate on the 2nd level: sizes
    foreach ($sizes as $sizeId => $quantity) {
        $cartWithData[] = [
            'shoe' => $shoesRepository->shoeDescr($shoeId),
            'shoePrice' => $shoesRepository->find($shoeId),
            'quantity' => $quantity,
            'size' => $stockRepository->shoeSizeCart($shoeId, $sizeId),
        ];
    }
}