function ppull($list, $property, $key_property)libphutil Technical Documentation (Core Utilities)
function ppull($list, $property, $key_property)
libphutil Technical Documentation (Core Utilities)
Access a property on a list of objects. Short for "property pull", this function works just like mpull(), except that it accesses object properties instead of methods. This function simplifies a common type of mapping operation:
$names = array(); foreach ($objects as $key => $object) { $names[$key] = $object->name; }
You can express this more concisely with ppull():
$names = ppull($objects, 'name');
ppull() 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->id] = $object->name; }
This is the ppull version():
$names = ppull($objects, 'name', 'id');
If you pass null as the second argument, the objects will be preserved:
$id_map = array(); foreach ($objects as $object) { $id_map[$object->id] = $object; }
With ppull():
$id_map = ppull($objects, null, 'id');
See also mpull(), which works similarly but calls object methods instead of accessing object properties.
Parameters
list | $list | Some list of objects. |
string|null | $property | Determines which **values** will appear in the result array. Use a string like 'name' to store the value of accessing the named property in each value, or `null` to preserve the original objects. |
string|null | $key_property | Determines how **keys** will be assigned in the result array. Use a string like 'id' to use the result of accessing the named property as each object's key, or `null` to preserve the original keys. |
Return
dict | A dictionary with keys and values derived according to whatever you passed as `$property` and `$key_property`. |
- Defined
- src/utils/utils.php:214