如果元素匹配,则合并数组 - 算法

时间:2014-06-11 02:06:15

标签: php arrays transformation

我在不同的商店里有一系列设备(ibm,sony,dell)(为了简化商店1,2,3)。我要做的是创建一个包含所有产品的商店的比较。在下面的示例中,唯一符合条件的商店是shop_id:2(商店1没有dell&商店3不卖ibm)。

示例:

[
    [  {"id": "1", "name": "ibm", "price": "22", "shop_id": "1"}, {"id": "2", "name": "ibm", "price": "27", "shop_id": "1"}, {"id": "3", "name": "ibm", "price": "21", "shop_id":" 2"} ],
    [  {"id": "4", "name": "sony", "price": "19", "shop_id": "1"}, {"id": "5", "name": "sony", "price": "21", "shop_id": "2"}, {"id": "6", "name": "sony", "price": "28", "shop_id": "3"} ],
    [  {"id": "7", "name": "dell", "price": "22", "shop_id": "2"}, {"id": "8", "name": "dell", "price": "27", "shop_id": "2"}, {"id": "9", "name": "dell", "price": "21", "shop_id": "3"} ]
]

必须转变为:

[
    [ {"id": "3", "name": "ibm", "price": "21", "shop_id": "2"}, {"id": "5", "name": "sony", "price": "21", "shop_id": "2"}, {"id": "7", "name": "dell", "price": "22", "shop_id": "2"}],
    [ {"id": "3", "name": "ibm", "price": "21", "shop_id": "2"}, {"id": "5", "name": "sony", "price": "21", "shop_id": "2"}, {"id": "8", "name": "dell", "price": "27", "shop_id": "2"}]  
]

我必须在PHP中完成它,但我需要的只是一种算法来解决这个问题。 有无限数量的设备 - 现在只有3家商店,但可能更多,因为它也是无限的。

到目前为止我的工作几乎可以工作......但是当一个商店中的设备有一个以上的交易时它会中断(它只获得第一笔交易)。

public function getDeals($prices){

    // define return array
    $multi_deals = array();

    // get number of devices 
    $no_devices = count($prices);

    // loop over each deal for first device
    foreach ($prices[0] as $dd){
        // reset other arrays
        for ($j=1; $j<$no_devices; $j++)
            reset($prices[$j]);

        // remember deal shop
        $shop = $dd['shop_id'];
        $success = true;
        $i=0;

        // remember deal
        $multi_deal = array();
        $multi_deal[$i] = $dd;

        // loop over rest of the devices 
        while ($i < ($no_devices-1)){
            $i++;
            // only continue if found a deal from the same shop
            if ( !($multi_deal[$i] = self::getDevice($shop, $prices[$i]))){
                $success = false;
                break;
            }    

            // THIS IS WHERE PRICES ARRAY WILL BE RESET IF THE SAME DEVICE IS TWICE IN ONE SHOP        
        }

        // we looped over all devices and find deals for each one of them
        if ($success)
            $multi_deals[] = $multi_deal;
    }
}
public function getDevice($current_shop, &$deals){
    while ($deal = next($deals)){
        if ($deal['shop_id'] == $current_shop)
            return $deal;
    }
    return false;
}

我现在试图让我的头围绕它几个小时,所以我会感激任何线索。

更新:

想象一下,你有供应商(商店)销售产品(ibm,sony,dell)。作为客户,我想买1 * ibm + 1 * sony + 1 * dell,它必须来自一家商店。

作为结果,我需要显示可能的交易的完整列表,按商店划分

在我给出的示例中,只有一家商店拥有所有免费产品 - shop_id:2。 此商店为戴尔提供2笔特卖。这就是为什么作为结果我们有两套可能的组合。

更新2:

我尝试了不同的方法,我想我越来越近了。我到了一个地方

[
    { "shop_id": "2", "deals": [ 
        { "ibm": [ 
            {"id": "3", "name": "ibm", "price": "21", "shop_id":" 2"} 
        ] },
        { "sony": [ 
            {"id": "5", "name": "sony", "price": "21", "shop_id": "2"} 
        ] },
        { "dell": [ 
            {"id": "7", "name": "dell", "price": "22", "shop_id": "2"}, 
            {"id": "8", "name": "dell", "price": "27", "shop_id": "2"}
        ] }
    ] }
]

再次转变为:

[
    [ {"id": "3", "name": "ibm", "price": "21", "shop_id": "2"}, {"id": "5", "name": "sony", "price": "21", "shop_id": "2"}, {"id": "7", "name": "dell", "price": "22", "shop_id": "2"}],
    [ {"id": "3", "name": "ibm", "price": "21", "shop_id": "2"}, {"id": "5", "name": "sony", "price": "21", "shop_id": "2"}, {"id": "8", "name": "dell", "price": "27", "shop_id": "2"}]  
]

1 个答案:

答案 0 :(得分:1)

Whoopie,这很有趣。特别是“扁平化”优惠阵列的一部分:-) 我有一个解决方案,不是在课堂上,但也许你可以使用它的一些元素。它非常灵活,因此您可以根据需要添加任意数量的商店和搜索项目。

$search = array('ibm','sony','dell','apple');   //the words to search for
$shop=array();  


//Populate shop array with first machine    
foreach($prices[0] as $offer){
    $shop[$offer['shop_id']][0][]=$offer;
    }

//Loop trough rest of machines
$t=1;
foreach($prices as $k=>$machine){
    if($k==0)continue; //skip the first one, we've done that
    $t++;
    foreach($machine as $offer){
          //if this shop_id not exists => continue 
    if(!isset($shop[$offer['shop_id']]))continue; 
    $shop[$offer['shop_id']][$k][]=$offer; //add offer to shop
    }
foreach($shop as $id=>$v){
          //if the count of machines not right => dump this shop
    if(count($v)!=$t){
         unset($shop[$id]);
         }
    }
}
//echo '<pre>'.print_r($shop,true).'</pre>';

$shop现在包含所有提供组合机器的商店,以及每个商店的一系列机器优惠。

//'Flatten' the shop-array, combining all possibilities     
$offers=array();
foreach($shop as $array){
    $return=array();
    foreach($array as $k=>$machine){

      //first machine: populate $return
      if($k==0){
         foreach($array[0] as $k=>$v){$return[$k][0]=$v;}
         continue;
         }

    $w=$return;
    $return=array();

    foreach($machine as $offer){
        foreach($w as $r){
        $r[]=$offer;
            $return[]=$r;
            }
        }
    }
    $offers=array_merge($offers,$return);
}

//echo '<pre>'.print_r($offers,true).'</pre>';

我用这个数组来测试:

$prices = array(
array(
array("id"=> "2", "name"=> "ibm", "price"=> "27", "shop_id"=> "11"), 
array("id"=> "3", "name"=> "ibm", "price"=> "21", "shop_id"=> "22"),
array("id"=> "10", "name"=> "ibm", "price"=> "44", "shop_id"=> "22"),
),
array(
array("id"=> "4", "name"=> "sony", "price"=> "19", "shop_id"=> "11"),
array("id"=> "5", "name"=> "sony", "price"=> "21", "shop_id"=> "22"),
array("id"=> "6", "name"=> "sony", "price"=> "28", "shop_id"=> "33"),
),
array (
array("id"=> "7", "name"=> "dell", "price"=> "22", "shop_id"=> "11"), 
array("id"=> "7", "name"=> "dell", "price"=> "44", "shop_id"=> "11"), 
array("id"=> "7", "name"=> "dell", "price"=> "22", "shop_id"=> "22"), 
array("id"=> "8", "name"=> "dell", "price"=> "27", "shop_id"=> "22"), 
array("id"=> "9", "name"=> "dell", "price"=> "21", "shop_id"=> "33")
),
array (
array("id"=> "17", "name"=> "apple", "price"=> "22", "shop_id"=> "11"), 
array("id"=> "17", "name"=> "apple", "price"=> "22", "shop_id"=> "22"), 
array("id"=> "18", "name"=> "apple", "price"=> "27", "shop_id"=> "22"), 
array("id"=> "19", "name"=> "apple", "price"=> "21", "shop_id"=> "33")
)
);