I'm looking for a way to replace nested foreach loops with a functional programming approach. Here is the sample data:
$mode[1] = [1, 2, 5, 6];
$mode[0] = [3, 4, 7, 8];
Currently my code is this:
foreach ($mode as $key => $value):
foreach ($value as $v):
$modes[$v] = $key;
endforeach;
endforeach;
echo '<pre>' . print_r($modes, 1) . '</pre>';
This generates the desired output (which can be thought of as flipping a 2d array):
array (
1 => 1,
2 => 1,
5 => 1,
6 => 1,
3 => 0,
4 => 0,
7 => 0,
8 => 0,
)
Does anyone know how the foreach code block can be replaced with a functional programming alternative?