How to remap array keys (based on values) with PHP?

Viewed 2679
<?php

// get unordered array of Group objects
$groups = $this->getDoctrine()->getManager()->getRepository('AcmeDemoBundle:Group');

$array = array();
// map array to $groupName => $groupObject
foreach ($groups as $group) {
    $array[$group->getName()] = $group;
}

Is there a shorter way to do that in PHP?

Note: Group names are unique, not empty strings.

I was thinking about array_walk, but I'm not sure if can replace the key somehow?

1 Answers
Related