Reputation: 383
I have two associative arrays, one is called $events and one is called $wikipedia.
I want an efficient way to join "extract" and "thumbnail" from $wikipedia to $events where "name" in $events matches "title" in $wikipedia.
Original $events
$events = Array
(
[0] => Array
(
[id] => 0
[name] => Human
[start] => 123
)
[1] => Array
(
[id] => 1
[name] => Neanderthal
[start] => 456
)
[2] => Array
(
[id] => 2
[name] => Denisovan
[start] => 456
)
)
$wikipedia
$wiki = Array
(
[26685803] => Array
(
[pageid] => 26685803
[ns] => 0
[title] => Denisovan
[thumbnail] => Array
(
[source] => https://upload.wikimedia.org/wikipedia/commons/thumb/c/ca/Denisova_4_Denisovan_molar_3.jpg/133px-Denisova_4_Denisovan_molar_3.jpg
[width] => 133
[height] => 200
)
[extract] => The Denisovans or Denisova hominins ( di-NEE-sə-və) are an extinct species or subspecies of archaic human that ranged across Asia during the Lower and Middle Paleolithic. Denisovans are known from few remains, and, consequently, most of what is known about them comes from DNA evidence. Pending consensus on their taxonomic status, they have been referred...
)
[682482] => Array
(
[pageid] => 682482
[ns] => 0
[title] => Human
[thumbnail] => Array
(
[source] => https://upload.wikimedia.org/wikipedia/commons/thumb/6/68/Akha_cropped_hires.JPG/119px-Akha_cropped_hires.JPG
[width] => 119
[height] => 200
)
[extract] => Humans (Homo sapiens) are a species of highly intelligent primates. They are the only extant members of the subtribe Hominina and—together with chimpanzees, gorillas, and orangutans—are part of the family Hominidae (the great apes, or hominids). Humans are terrestrial animals, characterized by their erect posture and bipedal locomotion; high manual...
)
[27298083] => Array
(
[pageid] => 27298083
[ns] => 0
[title] => Neanderthal
[thumbnail] => Array
(
[source] => https://upload.wikimedia.org/wikipedia/commons/thumb/d/d4/Range_of_NeanderthalsAColoured.png/200px-Range_of_NeanderthalsAColoured.png
[width] => 200
[height] => 95
)
[extract] => Neanderthals (, also Neandertals, Homo neanderthalensis or Homo sapiens neanderthalensis) are an extinct species or subspecies of archaic humans who lived in Eurasia until about 40,000 years ago. They most likely went extinct due to great climatic change, disease, or a combination of these factors.It is unclear when Neanderthals split from modern humans...
)
)
Modified $events with data from $wikipedia
$events = Array
(
[0] => Array
(
[id] => 0
[name] => Human
[start] => 123
[thumbnail] => Array
(
[source] => https://upload.wikimedia.org/wikipedia/commons/thumb/6/68/Akha_cropped_hires.JPG/119px-Akha_cropped_hires.JPG
[width] => 119
[height] => 200
)
[extract] => Humans (Homo sapiens) are a species of highly intelligent primates. They are the only extant members of the subtribe Hominina and—together with chimpanzees, gorillas, and orangutans—are part of the family Hominidae (the great apes, or hominids). Humans are terrestrial animals, characterized by their erect posture and bipedal locomotion; high manual...
)
[1] => Array
(
[id] => 1
[name] => Neanderthal
[start] => 456
[thumbnail] => Array
(
[source] => https://upload.wikimedia.org/wikipedia/commons/thumb/d/d4/Range_of_NeanderthalsAColoured.png/200px-Range_of_NeanderthalsAColoured.png
[width] => 200
[height] => 95
)
[extract] => Neanderthals (, also Neandertals, Homo neanderthalensis or Homo sapiens neanderthalensis) are an extinct species or subspecies of archaic humans who lived in Eurasia until about 40,000 years ago. They most likely went extinct due to great climatic change, disease, or a combination of these factors.It is unclear when Neanderthals split from modern humans...
)
[2] => Array
(
[id] => 2
[name] => Denisovan
[start] => 456
[thumbnail] => Array
(
[source] => https://upload.wikimedia.org/wikipedia/commons/thumb/c/ca/Denisova_4_Denisovan_molar_3.jpg/133px-Denisova_4_Denisovan_molar_3.jpg
[width] => 133
[height] => 200
)
[extract] => The Denisovans or Denisova hominins ( di-NEE-sə-və) are an extinct species or subspecies of archaic human that ranged across Asia during the Lower and Middle Paleolithic. Denisovans are known from few remains, and, consequently, most of what is known about them comes from DNA evidence. Pending consensus on their taxonomic status, they have been referred...
)
)
What I've tried
I could achieve this with a nested foreach loop. However this does not seem very efficient since I have to iterate every element in the $wikipedia array. I wonder if there might be a better way.
$arr = array();
foreach($events as $event){
foreach($wiki as $w){
if ($w['title'] == $event['name']){
$event['extract'] = $w['extract'];
$event['thumbnail'] = $w['thumbnail'];
array_push($arr, $event);
}
}
}
Upvotes: 0
Views: 119
Reputation: 1146
Remapping helps to avoid loops:
$map = array_column($wiki, null, 'title'); # remap the array
foreach ($events as ['id' => $id, 'name' => $name])
{
$events[$id]['extract'] = $map[$name]['extract'];
$events[$id]['thumbnail'] = $map[$name]['thumbnail'];
}
Upvotes: 1
Reputation: 1783
There is a more efficient way to this. Your solution will have runtime. Thats not very good if you need to join more than a few items.
The easiest way to improve on your solution is to generate a lookup table from the original data beforehand by creating an associative array which has title/name as it's key. When doing the merge afterwards a lookup will be fast (and does not get slower with more entries).
You only loop twice instead of n*m times. Which would lead to a linear runtime
$lookup = [];
$arr = [];
foreach($events as $event){
$lookup[$event['name']] = $event;
}
foreach($wiki as $w){
$event = $lookup[$w['title']];
if($event) {
$event['extract'] = $w['extract'];
$event['thumbnail'] = $w['thumbnail'];
array_push($arr, $event);
}
}
If you have a lot of data and need to preserve space, you can store the index of the item in the lookup table or remove the data from the original array after adding them to the lookup table.
Upvotes: 1