'Merge multiple multidimensional arrays by value

Problem

I have the following array, consisting of N different services, where each entry consists of an identifier and a unique (user)name.

$input = [
    'service_1' => [
        '1234' => 'John_Doe_1',
        '4567' => 'Jane Doe X',
        '7891' => 'J.Doe1',
    ],
    'service_2' => [
        '100001' => 'Jane Doe X',
        '100002' => 'John_Doe_1',
        '100003' => 'J.Doe1',
    ],
    'service_N' => [
        '07faed21-2920-4d7d-a263-88deba9c422c' => 'John_Doe_1',
        '1160178c-dfbf-4091-b4c0-a8ec55c22800' => 'J.Doe1',
    ],
];

Now I'm looking for a way to format it in a way that I get the identifiers across each (user)name for the different services:

$output = [
    'John_Doe_1' => [
        'service_1' => '1234',
        'service_2' => '100002',
        'service_N' => '07faed21-2920-4d7d-a263-88deba9c422c',
    ],
    'Jane Doe X' => [
        'service_1' => '4567',
        'service_2' => '100001',
        'service_N' => null, // either value should be null or key should not exist
    ],
    'J.Doe1' => [
        'service_1' => '7891',
        'service_2' => '100003',
        'service_N' => '1160178c-dfbf-4091-b4c0-a8ec55c22800',
    ],
];

I'm looking for a flexible way (with N services) to do this but I can't come up with a good solution.

php


Solution 1:[1]

I've been on a functional programming kick recently and figured I'd dive into PHP to see what I could come up with. Here's a nested array_walk method that seems to do the trick!

$output = Array();
array_walk($input, function($item, $key) use (&$output) {
    array_walk($item, function($item, $key, $parent_key) use (&$output) {
        $output[$parent_key][$item] = $key;
    }, $key);
});
var_dump($output);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1