【发布时间】:2011-07-26 03:21:24
【问题描述】:
我有点坚持以下功能。目标是: - 遍历包含所有产品的数组 - 根据luxury vs no_luxury划分产品 - 可用产品的另一种布局,我对可用产品有一个单独的函数,它返回一个关联数组。
我遇到的主要问题是我无法从 availableProducts 数组中解析价格值(和所有其他值)。我想我需要将 $availableProducts assoc 数组的相应 $values 添加到 $no_luxury 和 $luxury 数组中,或者从数组 $availableProducts 获取关联值 where =$value['productType']
或者我可以合并两个数组?
这是我的简化 php 代码
<?php
$staticData = getStaticData();
$availableProducts = getAvailableProducts();
function getAllProductTypes($availableProducts, $staticData) {
$allProductsTypes = array();
$luxury = array();
$no_luxury = array();
$no_luxury_products = array('CALL','DODO','PKK','YTK','TK');
//available products other layout with price and buy now button
$available = array();
//temp array for available products
foreach($availableProducts as $value ){
$available[] = $value['productType'];
//HOW TO STORE an assoc array
}
//get all products has no price value
$allProducts = getAllProducts();
foreach ($allProducts as $value) {
if(in_array($value["productType"], $no_luxury_products)){
$no_luxury[] = $value;
} else {
$luxury[] = $value;
}
}
//div no luxury
$return = '<div id="no_luxury" style="background-color:#0099FF;">';
foreach ($no_luxury as $value) {
//and productType is not availabe
if(!in_array($value['RoomType'],$available)){
$return .= 'test'.$value['productType'].': '.$value['productDescr'];
}//end not available
else {//products ara available we have a price
//problem i need to get the $value from the $availableProducts assoc array
$return .= 'Price from $availableProducts assoc Array '.$value['productPrice'].': '.$value['productDescr'].' by '.$staticData['owner'];
}
}
$return .= '</div>';
//end no luxury
//div luxury
$return .= '<div id="luxury" >';
foreach ($luxury as $value) {
//and productType is not availabe
if(!in_array($value['RoomType'],$available)){
$return .= 'test'.$value['productType'].': '.$value['productDescr'];
}//end not available
else {//products ara available we have a price
//problem i need to get the $value from the $availableProducts assoc array
$return .= 'Price from $availableProducts assoc Array '.$value['productPrice'].': '.$value['productDescr'].' by '.$staticData['owner'];
}
}
$return .= '</div>';
return $return;
}
有什么建议吗?
【问题讨论】:
-
您的数组是如何构成的?在特定的可用产品中,豪华和不豪华?
标签: php arrays foreach associative-array