Page MenuHomePhorge

function mpull($list, $method, $key_method)
libphutil Technical Documentation (Core Utilities)

Call a method on a list of objects. Short for "method pull", this function works just like ipull(), except that it operates on a list of objects instead of a list of arrays. This function simplifies a common type of mapping operation:

$names = array();
foreach ($objects as $key => $object) {
  $names[$key] = $object->getName();
}

You can express this more concisely with mpull():

$names = mpull($objects, 'getName');

mpull() takes a third argument, which allows you to do the same but for the array's keys:

$names = array();
foreach ($objects as $object) {
  $names[$object->getID()] = $object->getName();
}

This is the mpull version():

$names = mpull($objects, 'getName', 'getID');

If you pass null as the second argument, the objects will be preserved:

$id_map = array();
foreach ($objects as $object) {
  $id_map[$object->getID()] = $object;
}

With mpull():

$id_map = mpull($objects, null, 'getID');

See also ipull(), which works similarly but accesses array indexes instead of calling methods.

Parameters
list$listSome list of objects.
string|null$methodDetermines which **values** will appear in the result array. Use a string like 'getName' to store the value of calling the named method in each value, or ##null## to preserve the original objects.
string|null$key_methodDetermines how **keys** will be assigned in the result array. Use a string like 'getID' to use the result of calling the named method as each object's key, or `null` to preserve the original keys.
Return
dictA dictionary with keys and values derived according to whatever you passed as `$method` and `$key_method`.