【问题标题】:Loop through two assoc arrays and add values if needle is found to an array?循环遍历两个 assoc 数组并在找到 needle 的数组中添加值?
【发布时间】: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


【解决方案1】:

您还可以使用foreach($array as $key =&gt; $value ) 在遍历数组时获取密钥。

还有array_diff()array_intersect() 也可能对您有所帮助。

【讨论】:

  • tx,我会考虑你的建议。目前我通过使用 array_merge 找到了一个可行的解决方案
猜你喜欢
  • 2018-04-07
  • 2016-04-19
  • 2012-04-10
  • 1970-01-01
  • 2014-09-23
  • 1970-01-01
  • 2018-03-16
  • 2022-01-19
  • 1970-01-01
相关资源
最近更新 更多