【发布时间】:2017-07-19 14:03:55
【问题描述】:
我从这样的控制器中的某种形式获得两个参数:
$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
)
好的,现在我想进入 每个循环 productId 和 qty
我测试了它适用于产品,但我无法获得数量:
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
}
【问题讨论】:
-
如果你想连接
ids和qtys,那么对于每个id都有一个对应的qty(或者相反,它没有问题),您应该创建一个关联数组,其中$productId的值作为键,qty的值作为值,所以你会得到类似:[106 => 4, 107 => 3, ...]。然后,您可以访问这两个值,id作为键,qty作为foreach()中的值。 -
好主意,如何将两者关联起来?
array_merge?如果你能回答这个问题就好了 -
你要找的函数是array_combine()。
标签: php zend-framework zend-form