phparraysarray-flip

array_flip with an array in the value


Simple one (but not solved yet): this is my array

[
 [
   "role" => "admin",
   "name" => [
     "Felipy",
     "Ivan",
   ],
 ],
 [
   "role" => "user",
   "name" => [
     "Michel",
     "Paul",
     "Tay",
 ],
]

And I would love to see this result:

[
 "Felipy" => "admin",
 "Ivan" => "admin",
 "Michael" => "user",
 "Paul" => "user",
 "Tay" => "user",
]

I've already tried array_flip


Solution

  • Try this:

    foreach ($yourArr as $role) {
     foreach ($role['name'] as $user) {
        $arr[$user] = $role['role'];
     }
    }