Collection

@param array $categories
@return array
341501
public function getTree($categories = [])
    {
        /** @var $category Category */
        foreach ($this as $category) {
            $categories[] = $category;
            $categories = $category->getChildren()->notDeleted()->findAll()->getTree($categories);
        }

        return $categories;
    }
1130050
public function getAchievementsByCategory(Category $category)
    {
        $achievements = array();

        /** @var Achievement $achievement */
        foreach ($this->getAchievements() as $achievement) {
            if ($achievement->getCategory() == $category) {
                $achievements[] = $achievement;
            }
        }

        return $achievements;
    }
907964
public function getCategories()
    {
        if (null === $this->_categories) {

            $collection = Mage::getModel('catalog/category')->getCollection();

            $pathLike = Mage_Catalog_Model_Category::TREE_ROOT_ID . '/' . Mage::app()->getStore()->getRootCategoryId() . '/%';
            $collection
                ->initCache(Mage::app()->getCacheInstance(), self::class, [Mage_Catalog_Model_Category::CACHE_TAG])
                ->setStoreId(Mage::app()->getStore()->getId())
                ->addAttributeToSelect('*')
                ->addFieldToFilter('path', [
                    'like' => $pathLike,
                ])
                ->addIsActiveFilter()
                ->addAttributeToFilter('include_in_menu', 1)
                ->joinUrlRewrite();

            // Sort
            $collection->getSelect()->reset(Zend_Db_Select::ORDER);
            $collection
                ->addOrder('position', Varien_Data_Collection_Db::SORT_ORDER_ASC);

            foreach ($collection as $id => $category) {
                $this->_categories[$id] = $category;
            }
        }

        return $this->_categories;
    }
826936
public function getAll($category, $context)
    {
        $model = $this->getModel($category);

        return $model->getAll($context);
    }
1079658
protected function forCategories() : array
	{
		$categories = $this->products->pluck('category');

		return $categories->mapWithKeys(function ($item) use ($categories) {

				$result[$item->id] = [
					'id' => $item->id,
					'name' => $item->name,
					'qty' => $categories->where('id', $item->id)->count()
				];

				return $result;
		})->all();
	}
1049006
public static List<Group> categoriesToGroups(List<Category> categories) {
    return categories.stream()
        .map(Category::getGroups)     // get groups from categories
        .filter(Objects::nonNull)     // remove all null
        .flatMap(Collection::stream)
        .collect(Collectors.toList());
  }
1040670
public static function get_all_question_types_in_categories($categories) {
        global $DB;

        list($categorysql, $params) = $DB->get_in_or_equal($categories);
        $sql = "SELECT DISTINCT q.qtype
                FROM {question} q
                WHERE q.category $categorysql";

        $qtypes = $DB->get_fieldset_sql($sql, $params);
        return $qtypes;
    }
444927
protected function filter(array $categories)
    {
        // 过滤空的子分类
        foreach ($categories as $i => $category) {
            if (!isset($category['navs']) || !$category['navs']) {
                unset($categories[$i]);
                continue;
            }
            foreach ($category['navs'] as $j => $subCategories) {
                if (!isset($subCategories['navs']) || !$subCategories['navs']) {
                    unset($categories[$i]['navs'][$j]);
                }
            }
        }

        // 过滤空的父分类
        foreach ($categories as $i => $category) {
            if (!isset($category['navs']) || !$category['navs']) {
                unset($categories[$i]);
            }
        }

        return $categories;
    }
1083592
public function getModules(string $category) {
        if (isset($this->modules[$category])) {
            return $this->modules[$category];
        }

        return null;
    }
91669
public static function get_category_extensions($categories)
    {
        if (empty($categories)) {
            return array();
        }

        // Fix arguments into a single array
        if (!is_array($categories)) {
            $categories = array($categories);
        } elseif (count($categories) === 1 && is_array(reset($categories))) {
            $categories = reset($categories);
        }

        // Check configured categories
        $appCategories = static::config()->get('app_categories');

        // Merge all categories into list of extensions
        $extensions = array();
        foreach (array_filter($categories) as $category) {
            if (isset($appCategories[$category])) {
                $extensions = array_merge($extensions, $appCategories[$category]);
            } else {
                throw new InvalidArgumentException("Unknown file category: $category");
            }
        }
        $extensions = array_unique($extensions);
        sort($extensions);
        return $extensions;
    }
281038
public function getCategory($categories)
    {
        $this->validateNumericArray('categories', $categories);
        $stats = $this->getGlobal();
        $stats['categories'] = $categories;
        return $stats;
    }
178266
public static function create_categories($categories) {
        global $DB;

        $params = self::validate_parameters(self::create_categories_parameters(),
                        array('categories' => $categories));

        $transaction = $DB->start_delegated_transaction();

        $createdcategories = array();
        foreach ($params['categories'] as $category) {
            if ($category['parent']) {
                if (!$DB->record_exists('course_categories', array('id' => $category['parent']))) {
                    throw new moodle_exception('unknowcategory');
                }
                $context = context_coursecat::instance($category['parent']);
            } else {
                $context = context_system::instance();
            }
            self::validate_context($context);
            require_capability('moodle/category:manage', $context);

            // this will validate format and throw an exception if there are errors
            external_validate_format($category['descriptionformat']);

            $newcategory = core_course_category::create($category);
            $context = context_coursecat::instance($newcategory->id);

            $createdcategories[] = array(
                'id' => $newcategory->id,
                'name' => external_format_string($newcategory->name, $context),
            );
        }

        $transaction->allow_commit();

        return $createdcategories;
    }
440308
public function addCategorie(\Haven\CoreBundle\Entity\Category $categories)
    {
        $this->categories[] = $categories;
    
        return $this;
    }
471748
protected function sort($categories)
    {
        foreach ($categories as &$category) {
            // 对子分类排序
            $category['navs'] = $this->orderBy($category['navs']);

            // 对导航项排序
            foreach ($category['navs'] as &$subCategories) {
                $subCategories['navs'] = $this->orderBy($subCategories['navs']);
            }

            // 取第一个导航项的链接,作为父分类的链接
            $category['url'] = $category['navs'][0]['navs'][0]['url'];
        }

        // 对父分类排序
        return $this->orderBy($categories);
    }
694052
public function post(array $categories) {
    $storeId = '';
    $requestHeaders = $this->currentRequest->headers;
    if ($requestHeaders->has('X-ACM-UUID')) {
      $storeId = $requestHeaders->get('X-ACM-UUID');
    }

    if (!$this->categoryVid) {
      return (new ModifiedResourceResponse(['success' => FALSE]));
    }

    $response = $this->categoryManager->synchronizeCategory(
      $this->categoryVid,
      $categories,
      $storeId
    );

    $response['success'] = (bool) (($response['created'] > 0) || ($response['updated'] > 0));

    return (new ModifiedResourceResponse($response));
  }
703436
public function makeChildrenTree($categories)
    {
        $tmp = [];
        foreach ($categories as $key => $category){
            if(!key_exists('children', $category)) {
                $category->children = [];
            }

            // get children
            $children = $this->getChildren($category->categoryID);

            // call self to get a loop until every category has his children
            $category->children = $this->makeChildrenTree($children);

            $tmp[] = $category;
        }

        return Language::filterRows($tmp, false);
    }
1033590
private function prepareRatios(Collection $categories)
    {
        $ratios = $categories->filter(function (Category $category) {
                return $category->hasPosts();
            })
            ->transform(function (Category $category) {
                return [
                    'label' => $category->name,
                    'posts' => $category->posts->count(),
                ];
            })
            ->sortByDesc('posts');

        $chunk  = $ratios->splice(5);
        $ratios = $this->colorizeRatios($ratios);

        return $chunk->isEmpty() ? $ratios : $ratios->push([
            'label' => 'Other Categories',
            'posts' => $chunk->sum('posts'),
            'color' => '#D2D6DE',
        ]);
    }