Arek S
Arek S

Reputation: 4719

Merge array if element match - algorithm

I have an array of devices (ibm, sony, dell) in different shops (to simplify shops 1,2,3). What I have to do is to create a comparison of shops which have all products. In example below the only shop which match the condition is shop_id: 2 (shop 1 doesn't have dell & shop 3 doesn't sell ibm).

Example:

[
    [  {"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"} ]
]

has to be transformed to:

[
    [ {"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"}]  
]

I have to do it in PHP but all I need is an algorithm to solve this. There is an unlimited number of devices - only 3 shops for now but it might be more so assume it's unlimited too.

What I have so far almost work... but it breaks when there is more then one deal for a device in one shop (it gets only the first deal).

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;
}

I'm trying to get my head around it for few hours now, so I'll appreciate any clues.

UPDATE:

Imagine you have suppliers (shops) selling the products (ibm, sony, dell). As a customer I want to buy 1*ibm + 1*sony + 1*dell and it has to be from one shop.

As a results I need to show full list of possible deals, split by shops

In the example I gave there is only one shop which has all free products - shop_id: 2. What is more this shop have 2 deals for dell. That's why as a results we have two sets as there are two possible combinations.

UPDATE 2:

I've tried different approach and I think I'm getting closer. I got to a point where I have

[
    { "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"}
        ] }
    ] }
]

which again has to transformed into:

[
    [ {"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"}]  
]

Upvotes: 0

Views: 116

Answers (1)

Michel
Michel

Reputation: 4157

Whoopie, that was fun figuring out. Especially the part of 'flattening' the offers array :-) I have a solution for you, not in a class, but maybe you could use some elements of it. It is flexible, so you could add as many stores and searchitems as you like.

$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 now contains all the shops that have the combined machines on offer, and per shop an array of machine-offers.

//'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>';

I used this array to test:

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

Upvotes: 1

Related Questions