[man]array_walk_recursive[/man] would be more useful if the array were already in the form you're trying to get.
If I understand your sketch of your input data, it looks something like this:
array(
'user1' => array(a,b,c,d,e),
'user2' => array(1,2,3,user1),
'user3' => array(dog,cat,orange,user2)
);
And you want
array(
'user3' => array('dog'=>'', 'cat'=>'', 'orange'=>'',
'user2' => array('1'=>'', '2'=>'', '3'=>'',
'user1'=> array('a'=>'', 'b'=>'', 'c'=>'', 'd'=>'', 'e'=>'')));
);
I started with a recursive solution, then when I realised it was tail-recursive I unrolled it to work iteratively.
function condenseOwnership($accounts)
{
$accounts = array_map(function($f)
{
return array_fill_keys($f, '');
}, $accounts);
do
{
$did_something = false;
foreach($accounts as $user=>$account)
{
foreach(array_keys(array_intersect_key($accounts, $account)) as $subaccount)
{
$accounts[$user][$subaccount] = $accounts[$subaccount];
unset($accounts[$subaccount]);
$did_something = true;
}
}
} while($did_something);
return $accounts;
}
$accounts = array(
'user1' => array('a','b','c','d','e'),
'user2' => array('1','2','3','user1'),
'user3' => array('dog','cat','orange','user2')
);
condenseOwnership($accounts);
scrupulous wrote: Also, happy t-day
Huh? Oh ... right. Nope, nothing to do with me, mate.