通过一个循环获取两个数据

时间:2017-07-19 14:03:55

标签: php zend-framework zend-form

我在控制器中从某种形式得到两个参数:

$productIds = $this->getRequest()->getParam('productId');
$productQtys = $this->getRequest()->getParam('qty');

结果:

[productId] => Array(
               [0] => 106
               [1] => 107
               [2] => 108
               [3] => 109
               )    
[qty] => Array(
               [0] => 4
               [1] => 3
               [2] => 2
               [3] => 1
              )

好,现在我想进入每个循环一个productIdqty

我测试了它适用于产品,但我无法获得数量:

foreach($productIds as $prod) {
    //some code...
    $product = Mage::getModel('catalog/product')
                     ->setStoreId(Mage::app()->getStore()->getId())
                     ->load($prod);
    $cart->addProduct($product->getId(), $qty) // here how i get the $qty of each product, if i put it manually it works, 2 for exemple, it add 2 for all products

}

2 个答案:

答案 0 :(得分:0)

array_combine($productIds,$productQtys)

结果:

array(
       [1639] => 6
       [1640] => 4
       [1641] => 3
       [1646] => 2
)

//Thanks to @Tobias F

答案 1 :(得分:0)

如果您的密钥是对应的,那么您可以将foreach扩展为key =>值构造。然后你可以直接在qty数组中使用key。没有测试它,但在这里:

foreach($productIds as $key => $prod) {

    $product = Mage::getModel('catalog/product')
                     ->setStoreId(Mage::app()->getStore()->getId())
                     ->load($prod);
    $cart->addProduct($product->getId(), $qty[$key]) 
} 
相关问题