<?php
$arr = array(
2 => 'John',
21 => 'John',
32 => 'Joe',
42 => 'John',
23 => 'Joe',
62 => 'John',
25 => 'Joe',
);
$flipped = [];
foreach($arr AS $key => $value) {
$flipped[$value][] = $key;
}
print_r($flipped);
- Output for 5.5.0 - 5.5.36, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.31, 8.2.0 - 8.2.26, 8.3.0 - 8.3.14, 8.4.1
- Array
(
[John] => Array
(
[0] => 2
[1] => 21
[2] => 42
[3] => 62
)
[Joe] => Array
(
[0] => 32
[1] => 23
[2] => 25
)
)
preferences:
79.78 ms | 408 KiB | 5 Q