Skip to main content

Reduce nested loops with closures
and functional programming

Published on 2021-05-12

Today I was developing some code that combined information from 2 different arrays. The first array was a list of data-objects. Each data-object has a 'type' parameter. The second array was a list of 'types' and it function is to specify the order (sorting) and which data-object must be rendered. Initially I solved this with 2 nested foreach loops, which hurts the eyes and can be a performance killer when working with big data-structures. So in a code-review my collegue asked me to rewrite this.

The data

Array 1 with data objects


$array_1 = [
  ['type' => 'facebook', ...],
  ['type' => 'instagram', ...],
  ['type' => 'reddit', ...],
  ['type' => 'twitter', ...],
  ['type' => 'hacker news', ...],
];

Array 2 with what to render in what sorting


$array_2 = ['hacker news', 'facebook', 'reddit'];

Array end result


[
  ['type' => 'hacker news', ...],
  ['type' => 'facebook', ...],
  ['type' => 'twitter', ...],
];

Solved with functional programming style


// Remove types from array_1 that are not in array_2.
$array_1 = array_filter($array_1, static function ($item) use ($array_2) {
  return in_array($item['type'], $array_2, TRUE);
});

// Apply array_2 sorting on array_1
usort($array_1, static function ($a, $b) use ($array_2) {
  $pos_a = array_search($a['type'], $array_2, TRUE);
  $pos_b = array_search($b['type'], $array_2, TRUE);
  return $pos_a - $pos_b;
});