dok300 Posted July 31, 2013 Share Posted July 31, 2013 I have a multidimensional array like so. Array ( [0] => Array ( [0] => Yolo County Sheriff's Home 2008faroo [1] => http://www.yolosheriffs.com/ [2] => 86 ) [1] => Array ( [0] => Fremont, Yolo County, California - Wikipedia, the free encyclopediafaroo [1] => http://en.wikipedia.org/wiki/Fremont,_Yo… [2] => 11 ) [2] => Array ( [0] => The Lonely Island - YOLO (feat. Adam Levine & Kendrick Lamar) - YouTubefaroo [1] => http://www.youtube.com/watch?feature=pla… [2] => 45 ) What i need to do is find duplicate urls in this array, merge them together and combine the score found at [2]. I have scoured the php manual but I can't seem to find a way to merge the duplicate urls and then add the scores to the remaining url. Would it make it any easier to have it in the format Array ( [0] => Array ( [0] => Yolo County Sheriff's Home 2008faroo, array([0] => http://www.yolosheriffs.com/ ,[1] => 0) ) ?? All suggestions appreciated.I have looked at the manual but i don't have the knowledge at this stage to use the array functions in conjunction with foreach loops to achieve what i need.I Link to comment https://forums.phpfreaks.com/topic/280657-merging-elements-of-an-multidimensional-array-together-and-incrementing-value/ Share on other sites More sharing options...
requinix Posted July 31, 2013 Share Posted July 31, 2013 There's a fairly standard way of doing this de-duplication process when the "key" is a simple value (like a string URL). 1. Start with an empty array for the de-duplicated data 2. Loop through the source array 3. If isset($deduplicated_array[$unique_key_value]) then "merge" the new value into the existing value. In your case that's adding to the [2]. 4. If not set then add it with $deduplicated_array[$unique_key_value] = $original_source_data. In your case you end up with an array array( "http://www.yolosheriffs.com/" => array( 0 => "Yolo County Sheriff's Home 2008faroo", 1 => "http://www.yolosheriffs.com/", 2 => 86 ), "http://en.wikipedia.org/wiki/Fremont,_Yo" => array( 0 => "Fremont, Yolo County, California - Wikipedia, the free encyclopediafaroo", 1 => "http://en.wikipedia.org/wiki/Fremont,_Yo", 2 => 11 ), "http://www.youtube.com/watch?feature=pla" => array( 0 => "The Lonely Island - YOLO (feat. Adam Levine & Kendrick Lamar) - YouTubefaroo", 1 => "http://www.youtube.com/watch?feature=pla", 2 => 45 ) ) Link to comment https://forums.phpfreaks.com/topic/280657-merging-elements-of-an-multidimensional-array-together-and-incrementing-value/#findComment-1442737 Share on other sites More sharing options...
dok300 Posted July 31, 2013 Author Share Posted July 31, 2013 This is simply fantastic, after spending a day and a half looking up arr_diff_assoc etc in the manual and getting decimated on SO it gets solved in about 15 minutes. Thanks. Link to comment https://forums.phpfreaks.com/topic/280657-merging-elements-of-an-multidimensional-array-together-and-incrementing-value/#findComment-1442738 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.