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


Merge array if element match - algorithm

我在不同的商店(简化商店1、2、3)有一系列设备(ibm、索尼、戴尔)。我要做的是对所有产品的商店进行比较。在下面的例子中,唯一符合条件的商店是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,索尼,戴尔)。作为一名顾客,我想买1*ibm + 1*索尼+ 1*戴尔,而且必须从一家商店购买。

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

在我给出的例子中,只有一个商店有所有免费产品- shop_id: 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"}]  
]

哇哦,这很有趣。特别是"扁平化"报价数组的部分:-)我有一个解决方案,不是在课堂上,但也许你可以使用它的一些元素。它是灵活的,所以你可以添加尽可能多的商店和搜索项目,因为你喜欢。

$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")
)
);