/** * Similar to key by property however each entry is an array of matching collection items. If two items had the * same property value, both with appear in the same collection subset. Also items will NOT be removed if they * have missing properties * @param array $collection * @param string|array $property_to_key_by Either the name of a single property or an array of property names to * string together to create a key * @return array An array keyed by the given properties where each value in the returend array is a collection */ public static function groupByProperty(array $collection, $property_to_key_by) { $result = []; $properties_to_key_by = !is_array($property_to_key_by) ? [$property_to_key_by] : $property_to_key_by; foreach ($collection as $item) { $key = implode(".", ArrayUtility::dotReadProperties($item, $properties_to_key_by)); if (!isset($result[$key])) { $result[$key] = []; } $result[$key][] = $item; } return $result; }