Created
February 19, 2011 14:52
-
-
Save samshull/835099 to your computer and use it in GitHub Desktop.
Answer to the stackoverflow question http://stackoverflow.com/questions/3678457/how-to-array-merge-a-dynamic-array-based-on-one-of-its-value-similarity/5049383#5049383
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$_Retreived = array( | |
"website1.com" => array( | |
array('60" BRAVIA LX900 Series 3D HDTV', 'website1.com', 5299.99), | |
array('52" BRAVIA LX900 Series 3D HDTV', 'website1.com', 4499.99), | |
array('46" BRAVIA LX900 Series 3D HDTV', 'website1.com', 3699.99), | |
array('40" BRAVIA LX900 Series 3D HDTV', 'website1.com', 2999.99) | |
), | |
"website2.com" => array( | |
array('Sony 3D 60" LX900 HDTV BRAVIA', 'website2.com', 5400.99), | |
array('Sony 3D 52" LX900 HDTV BRAVIA', 'website2.com', 4699.99), | |
array('Sony 3D 46" LX900 HDTV BRAVIA', 'website2.com', 3899.99), | |
), | |
"website3.com" => array( | |
array('Sony 40" BRAVIA LX900 3D HDTV', 'website3.com', 999.00) | |
), | |
"website4.com" => array( | |
array('60" BRAVIA LX900 Series 3D HDTV', 'website4.com', 888.00) | |
), | |
"website5.com" => array( | |
array('32" BRAVIA LX900 Series 3D HDTV', 'website4.com', 888.00) | |
), | |
); | |
$Product_Info = find_products($_Retreived); | |
$Prices = array_values($Product_Info); | |
print_r($Prices); | |
// print_r($Product_Info); | |
function find_products($_Retreived) { | |
$Product_Info = array(); | |
foreach($_Retreived as $site1 => $products1) { | |
foreach(array_reverse($_Retreived, true) as $site2 => $products2) { | |
if ($site1 !== $site2) { | |
foreach ($products1 as $index1 => $product1) { | |
list($name1, $site1, $price1) = $product1; | |
foreach ($products2 as $index2 => $product2) { | |
list($name2, $site2, $price2) = $product2; | |
if ($common_name = get_common_name($name1, $name2)) { | |
if (strstr($common_name, '"')) { | |
$Product_Info[$common_name]['Name'] = $name1; | |
$Product_Info[$common_name][$site1] = $price1; | |
$Product_Info[$common_name][$site2] = $price2; | |
// remove the matched products | |
unset($_Retreived[$site1][$index1], $_Retreived[$site2][$index2]); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
foreach($_Retreived as $site => $products) { | |
foreach($products as $data) { | |
if (!isset($Product_Info[$data[0]])) { | |
$Product_Info[$data[0]] = array( | |
'Name' => $data[0], | |
$data[1] => $data[2], | |
); | |
} | |
} | |
} | |
return $Product_Info; | |
} | |
function get_common_name($name1, $name2) { | |
// replace non-word and non-whitespace chars, then split on whitespace | |
$parts1 = preg_split('/\s+/', $name1); | |
$parts2 = preg_split('/\s+/', $name2); | |
// find the common words | |
$intersect = array_intersect($parts1, $parts2); | |
sort($intersect); | |
// remove stop words | |
return !empty($intersect) ? trim(preg_replace('/\s*Series\s*/', ' ', implode(' ', $intersect))) : false; | |
} | |
/* | |
Array | |
( | |
[0] => Array | |
( | |
[Name] => 60" BRAVIA LX900 Series 3D HDTV | |
[website1.com] => 5299.99 | |
[website4.com] => 888 | |
[website2.com] => 5400.99 | |
) | |
[1] => Array | |
( | |
[Name] => 40" BRAVIA LX900 Series 3D HDTV | |
[website1.com] => 2999.99 | |
[website3.com] => 999 | |
) | |
[2] => Array | |
( | |
[Name] => 52" BRAVIA LX900 Series 3D HDTV | |
[website1.com] => 4499.99 | |
[website2.com] => 4699.99 | |
) | |
[3] => Array | |
( | |
[Name] => 46" BRAVIA LX900 Series 3D HDTV | |
[website1.com] => 3699.99 | |
[website2.com] => 3899.99 | |
) | |
[4] => Array | |
( | |
[Name] => 32" BRAVIA LX900 Series 3D HDTV | |
[website4.com] => 888 | |
) | |
) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment