0

Is there any predefined function in any version of PHPs which could do this:

I have this array:

$arr = array(
    2 => 'John',
    21 => 'John',
    32 => 'Joe',
    42 => 'John',
    23 => 'Joe',
    62 => 'John',
    25 => 'Joe',
    );

I expect this result:

array(
    'John' => array(2, 21, 42, 62),
    'Joe' => array(32, 23, 25),
);

array_flip doesn't do what I want.

Note: I can write a custom code to do that and it's easy. My question is if there is a function in PHP can do it?

Last update: I think the answer should be NO

Mojtaba
  • 4,852
  • 5
  • 21
  • 38

2 Answers2

6

This is solved quite simply with a foreach loop

$flipped = [];
foreach($arr AS $key => $value) {
    $flipped[$value][] = $key;
}

Working example: https://3v4l.org/aA826

No it's not a function. But it's simple, why make it more complicated?

jszobody
  • 28,495
  • 6
  • 61
  • 72
  • 3
    Exactly. This is the best way. – Ryan Jun 08 '16 at 16:26
  • I was intrigued about that; turns out you don't get an error at all! – cmbuckley Jun 08 '16 at 16:40
  • 1
    Because it's assignment. If I was trying to retrieve an array index that doesn't exist, I'd get undefined. Assignment works though. – jszobody Jun 08 '16 at 16:40
  • @Thamilan yep! except we now have short array syntax, yay. – jszobody Jun 08 '16 at 16:45
  • @jszobody I never downvote any answer.. I upvoted yours both first. I downvote only questions that are duplicates – Thamilhan Jun 08 '16 at 16:46
  • I didn't want a custom code. Please delete your answer. I am going to delete this question. tnx. – Mojtaba Jun 08 '16 at 16:46
  • @Mojtaba you shouldn't delete a question after answers have been received. You showed the result you needed, and two people here helped you get it. – jszobody Jun 08 '16 at 16:48
  • @Thamilan my bad, was just the timing that made me think that. – jszobody Jun 08 '16 at 16:48
  • @jszobody the surprising lack of error is that you can essentially do `$foo[] = 1` when `$foo` is undefined. – cmbuckley Jun 08 '16 at 16:49
  • @jszobody, These 2 people didn't read my request carefully. It was easy for me to use a loop to create that. I asked in my question I am looking for a PHP function. Not a custom code. – Mojtaba Jun 08 '16 at 16:50
  • 1
    If you just wanted the answer yes or no, couldn't you have read the documentation to determine that? – cmbuckley Jun 08 '16 at 16:52
4

You could do this with array_reduce:

$flipped = array_reduce(array_keys($arr), function ($carry, $item) use ($arr) {   
    $carry[$arr[$item]][] = $item;
    return $carry;
}, array());
cmbuckley
  • 40,217
  • 9
  • 77
  • 91
  • I didn't want a custom code. Please delete your answer. I am going to delete this question. tnx. – Mojtaba Jun 08 '16 at 16:46
  • 2
    @Mojtaba to be fair, this answer is using a predefined function to do exactly what you want. even if I think a loop is simpler, this answer is technically what you asked for. you should probably accept it. – jszobody Jun 08 '16 at 16:52
  • @jszobody, Thanks. However, it's a custom code with a closure function. – Mojtaba Jun 08 '16 at 16:57