Skip to content
Advertisement

Join two associative arrays in PHP

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

JavaScript

$wikipedia

JavaScript

Modified $events with data from $wikipedia

JavaScript

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.

JavaScript

Advertisement

Answer

Remapping helps to avoid loops:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement