Jump to content

Merging elements of an multidimensional array together and incrementing value.


dok300

Recommended Posts

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

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
	)
)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.