Skip to content

Instantly share code, notes, and snippets.

@kriswallsmith
Created September 10, 2013 19:54
Show Gist options
  • Save kriswallsmith/6514704 to your computer and use it in GitHub Desktop.
Save kriswallsmith/6514704 to your computer and use it in GitHub Desktop.
I could really go for an array_index() function right now.
<?php
$widgets = array_combine(
array_map(
function($widget) {
return $widget->getId();
},
$widgets
),
$widgets
);
<?php
$widgets = array_index(function($widget) {
return $widget->getId();
}, $widgets);
@bobthecow
Copy link

After-after :)

widgets = Hash[widgets.map { |w| [w.id, w] }]

@axelarge
Copy link

Shameless plug!
You should try my ArrayTools package: https://github.com/axelarge/php-array-tools
Available via composer: https://packagist.org/packages/axelarge/array-tools

$widgets = Arr::indexBy($widgets, function ($widget) { return $widget->getId(); });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment