我正在使用现有的foreach,它正在逐步从数据库中提取一系列值。我想基于每个键/值对执行一些逻辑,并将一些值分配给变量,以作为foreach循环的一部分输出。
具体而言,我需要输出基于每个键/对的某些值确定的可用性。我的逻辑有效,但最后一条记录的值被分配给通过条件测试的每条记录,即使这些值不同。
如何通过循环每次正确分配$ value ['availability']?
这是我的代码:
foreach ($prices as $value) {
if(!$this->_validateAttributeValue($attributeId, $value, $options)) {
continue;
}
$currentProduct->setConfigurablePrice(
$this->_preparePrice($value['pricing_value'], $value['is_percent'])
);
$currentProduct->setParentId(true);
Mage::dispatchEvent(
'catalog_product_type_configurable_price',
array('product' => $currentProduct)
);
$configurablePrice = $currentProduct->getConfigurablePrice();
if (isset($options[$attributeId][$value['value_index']])) {
$productsIndex = $options[$attributeId][$value['value_index']];
} else {
$productsIndex = array();
}
/* Check the quantity */
if($options['qty'][$value['label']] <= 0) {
if ($product->getResource()->getAttribute('item_status')->getFrontend()->getValue($product) == "Preorder") { // Preorder
$value['availability'] = "Pre-order";
}
elseif ($product->getResource()->getAttribute('item_status')->getFrontend()->getValue($product) != "Discontinued") { // Must be an active backorder item
if ($product->getResource()->getAttribute('new_availability')->getFrontend()->getValue($product) != "No") {
$value['availability'] = $product->getResource()->getAttribute('new_availability')->getFrontend()->getValue($product);
}
else {
$value['availability'] = "Temporarily out of stock";
}
}
}
$info['options'][] = array(
'id' => $value['value_index'],
'label' => ($options['qty'][$value['label']] <= 0) ? $value['label'] . ': '.$value['availability'].'' : $value['label'] . ": In stock",
'price' => $configurablePrice,
'oldPrice' => $this->_prepareOldPrice($value['pricing_value'], $value['is_percent']),
'products' => $productsIndex,
);
$optionPrices[] = $configurablePrice;
}
答案 0 :(得分:0)
尝试
foreach ($prices as $key=>$value){
//your code
}