public static function reduceToElementsWithKey($array, $key) {

        if ($array instanceof AssociativeArray) {
            $array = $array->toArray();
        }

        $targetArray = array();

        foreach ($array as $itemKey => $value) {

            if ($itemKey == $key) {
                $targetArray[$key] = $value;
            } else if (is_array($value) || $value instanceof AssociativeArray) {
                $subArray = ArrayUtils::reduceToElementsWithKey($value, $key);
                if (sizeof($subArray) > 0) {
                    $targetArray[$itemKey] = $subArray;
                }
            }

        }

        return $targetArray;

    }