我得到某种形式的两个参数的控制是这样的:得到两个数据与一个循环
$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
}
如果你想连接'id's和'qty's,那么每个'id'都有一个对应的'qty'(或者其他方式,无所谓),您应该创建一个关联数组,其中'$ productId'的值作为键和'qty'的值作为值,所以您得到如下形式:'[106 => 4 ,107 => 3,...]。然后你可以在'foreach()'中访问这两个值,'id'作为关键字,'qty'作为值。 –
好主意,如何将两者联系起来? 'array_merge'?如果你可以回答这个问题,那就太好了 – mya
你正在寻找的函数是[array_combine()](http://php.net/manual/en/function.array-combine.php)。 –