array_flip с массивом в значении

Простой (но еще не решенный): это мой массив

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

И я хотел бы увидеть этот результат:

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

Я уже пробовала array_flip

-1

Решение

Попробуй это:

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

Другие решения

попробуй это DEMO

$original = [
[
"role" => "admin",
"name" => [
"Felipy",
"Ivan",
],
],
[
"role" => "user",
"name" => [
"Michel",
"Paul",
"Tay",
],
]
];
$arr = [];

foreach ($original as $value)
{
foreach($value['name'] as $name)
{
$arr[$name] = $value['role'];
}
}
1

Попробуй это Живая демо

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

$result = array_reduce($array, function($carry, $key) {
foreach ($key['name'] as $name)
$carry[$name] = $key['role'];
return $carry;
}, []);

var_dump($result);
1

Менее понятное, но все еще работающее решение:

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

$r = [];
foreach ($a as $item) {
$r = array_merge($r, array_fill_keys($item['name'], $item['role']));
}
echo'<pre>',print_r($r),'</pre>';
0
По вопросам рекламы [email protected]