Reputation: 987
I have this multidimensional array:
$arr = [
2 => ["a", "b", "c"],
5 => ["j", "k", "l"],
9 => ["w", "x", "y", "z"]
];
For which I'd like to create a new index array like this one:
$index = [
"a" => 2,
"b" => 2,
"c" => 2,
"j" => 5,
"k" => 5,
"l" => 5,
"w" => 9,
"x" => 9,
"y" => 9,
"z" => 9
]
I couldn't find any PHP function that appears to do that, but I'm sure there is one. Or maybe there's some known code that does this efficiently?
Upvotes: 0
Views: 50
Reputation: 32517
$index = array();
foreach ($arr as $k => $a) {
foreach ($a as $v) {
$index[$v] = $k;
}
}
Upvotes: 4