<p>You might just have to do it yourself :)</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
$criteria-&gt;group = 'groupHandle';
$categories = $criteria-&gt;find();
$sortedCategories = [];

foreach ($categories as $category) {
  if (!isset($sortedCategories[$category-&gt;level]) {
    $sortedCategories[$category-&gt;level] = [];
  }
  $sortedCategories[$category-&gt;level][] = $category;
}
var_dump($sortedCategories);
</code></pre>

<p>NOTE: I haven't tested this code, but in theory it should do the trick :)</p>

CC BY-SA 3.0
2018-01-15T10:35:37.040Z
2018-01-15T10:35:37.040Z
Fuglsetrampen
<p>I'm making a plugin and I need to get an array from a certain category group where the levels of the array are the same as the level of the categories in the group.</p>

<p>So I used the following code to get all categories in a group.</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
$criteria-&gt;group = 'groupHandle';
$array = $criteria-&gt;find();`
</code></pre>

<p>This works fine but I want to create something like this:</p>

<pre><code>$array[
    level1:[ 
        level2[
            level3,
            level3,
        ],
        level2[], 
        level2[]
    ],
    level1:[ 
        level2[],
        level2[], 
        level2[]
    ]
]
</code></pre>

<p>I hope you get the idea...
I could make a foreach in a foreach in a foreach but that's not very classy.
Thanks in advance!</p>

<plugin-development><categories><elementcriteriamodel><controller><array>
Store categories in array by level in controller
# Store categories in array by level in controller

(Asked by: username_0 on 2017-06-06)

I'm making a plugin and I need to get an array from a certain category group where the levels of the array are the same as the level of the categories in the group.
So I used the following code to get all categories in a group.
$criteria = craft()->elements->getCriteria(ElementType::Category);
$criteria->group = 'groupHandle';
$array = $criteria->find();`

This works fine but I want to create something like this:
$array[
    level1:[ 
        level2[
            level3,
            level3,
        ],
        level2[], 
        level2[]
    ],
    level1:[ 
        level2[],
        level2[], 
        level2[]
    ]
]

I hope you get the idea...
I could make a foreach in a foreach in a foreach but that's not very classy.
Thanks in advance!

**Tags:** plugin-development, categories, elementcriteriamodel, controller, array

---

## 1 answers

### Answer by username_1 on 2018-01-15. Score: 1

You might just have to do it yourself :)
$criteria = craft()->elements->getCriteria(ElementType::Category);
$criteria->group = 'groupHandle';
$categories = $criteria->find();
$sortedCategories = [];

foreach ($categories as $category) {
  if (!isset($sortedCategories[$category->level]) {
    $sortedCategories[$category->level] = [];
  }
  $sortedCategories[$category->level][] = $category;
}
var_dump($sortedCategories);

NOTE: I haven't tested this code, but in theory it should do the trick :)


---

