ioleo
ioleo

Reputation: 4817

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

<?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?

Upvotes: 4

Views: 2877

Answers (1)

ioleo
ioleo

Reputation: 4817

It seems there is no shorter/better way than the one posted in the question.

Upvotes: 2

Related Questions