<p>If you encounter problems with assigning variables as the keys, what about putting all your values into objects?</p>

<pre class="lang-twig prettyprint-override"><code>{% set catObjects = [] %}

{% for cat in allCats %}

    {% set catObject = [
        { catId: cat.id, catCount: craft.entries.relatedTo(cat).total()  }
    ] %}

    {% set catObjects = catObjects|merge(catObject) %}

{% endfor %}
</code></pre>

CC BY-SA 3.0
2014-07-30T20:43:50.990Z
2014-07-30T20:43:50.990Z
carlcs
<p>I have modified the code from <a href="https://craftcms.stackexchange.com/questions/1086/order-tags-by-count-e-g-top-10-tags-used">this question</a> but I'm running into an issue. </p>

<pre><code>{% set allCats = craft.categories.group('blogCategories').level(1).find() %}

{% set catArray = {} %}

{% for cat in allCats %}

    {% set catArray = catArray | merge({(cat.id):craft.entries.relatedTo(cat).total() }) %}

{% endfor %}

{% set catArray = catArray|sort|reverse %}

{% for catId, count in catArray %}

    {% set category = craft.categories.id(catId).first() %}

    &lt;li&gt;
        &lt;a href="{{ category.url }}"&gt;{{ category.title }} &lt;span&gt;{{count}}&lt;/span&gt;&lt;/a&gt;
    &lt;/li&gt;

{% endfor %}
</code></pre>

<p>Ive ran a dump on the catArray array and the category id isnt being set as the key, regardless of if I try put brackets around it to protect it, I only get the value in the array.</p>

CC BY-SA 3.0
2014-07-30T20:12:05.003Z
Did you try the code that I provided (totally untested from my side) in this other Q/A? Ohh and I think we should better close this thread here and "discuss" your problems over there...
carlcs
CC BY-SA 3.0
2014-07-30T20:12:44.173Z
possible duplicate of [Order tags by count - e.g. Top 10 Tags Used](http://craftcms.stackexchange.com/questions/1086/order-tags-by-count-e-g-top-10-tags-used)
carlcs
CC BY-SA 3.0
2014-07-30T20:20:24.113Z
Sean, I wonder why I (and you too) put the `cat.id` in brackets? Actually no reason to do this..
carlcs
CC BY-SA 3.0
2014-07-30T20:27:37.903Z
I don't think it's identical. The context is different and the issue I have hasn't been mentioned there. The reason you (and therefore I) have it in parentheses is due to [how Twig treats keys](http://stackoverflow.com/questions/14638231/twig-using-variables-as-hash-key). It does work when I use a `category.slug` as a key, but `category.id` does not want to work.
Seán O'Grady
CC BY-SA 3.0
2014-07-30T20:30:17.103Z
Yes not really completely a duplicate, but if you are running into problems those exact problems are laying in that other Q/A, too.
carlcs
CC BY-SA 3.0
2014-07-30T20:33:52.197Z
I think the issue isnÄt mentioned there because nobody actually tested the code :( ...I wonder who made those upvotes then, hehe?! Ohh and thanks for reminding me where those brackets come from.
carlcs
CC BY-SA 3.0
2014-07-30T20:44:51.557Z
Strange that it doesn't work, Sean. But I have an idea how it should definitely work! See answer below!
carlcs
<templating><array>
Order a list of categories by number of entries, key not being set
# Order a list of categories by number of entries, key not being set

(Asked by: username_0 on 2014-07-30)

I have modified the code from this question but I'm running into an issue. 
{% set allCats = craft.categories.group('blogCategories').level(1).find() %}

{% set catArray = {} %}

{% for cat in allCats %}

    {% set catArray = catArray | merge({(cat.id):craft.entries.relatedTo(cat).total() }) %}

{% endfor %}

{% set catArray = catArray|sort|reverse %}

{% for catId, count in catArray %}

    {% set category = craft.categories.id(catId).first() %}

    
        {{ category.title }} {{count}}
    

{% endfor %}

Ive ran a dump on the catArray array and the category id isnt being set as the key, regardless of if I try put brackets around it to protect it, I only get the value in the array.

**Tags:** templating, array

> **Comments:**
> **username_1**: Did you try the code that I provided (totally untested from my side) in this other Q/A? Ohh and I think we should better close this thread here and "discuss" your problems over there...  
> **username_1**: possible duplicate of [Order tags by count - e.g. Top 10 Tags Used](http://craftcms.stackexchange.com/questions/1086/order-tags-by-count-e-g-top-10-tags-used)  
> **username_1**: Sean, I wonder why I (and you too) put the `cat.id` in brackets? Actually no reason to do this..  
> **username_0**: I don't think it's identical. The context is different and the issue I have hasn't been mentioned there. The reason you (and therefore I) have it in parentheses is due to [how Twig treats keys](http://stackoverflow.com/questions/14638231/twig-using-variables-as-hash-key). It does work when I use a `category.slug` as a key, but `category.id` does not want to work.  
> **username_1**: Yes not really completely a duplicate, but if you are running into problems those exact problems are laying in that other Q/A, too.  
> **username_1**: I think the issue isnÄt mentioned there because nobody actually tested the code :( ...I wonder who made those upvotes then, hehe?! Ohh and thanks for reminding me where those brackets come from.  
> **username_1**: Strange that it doesn't work, Sean. But I have an idea how it should definitely work! See answer below!  

---

## 1 answers

### Answer by username_1 on 2014-07-30. Score: 2 | Accepted answer

If you encounter problems with assigning variables as the keys, what about putting all your values into objects?
{% set catObjects = [] %}

{% for cat in allCats %}

    {% set catObject = [
        { catId: cat.id, catCount: craft.entries.relatedTo(cat).total()  }
    ] %}

    {% set catObjects = catObjects|merge(catObject) %}

{% endfor %}


---

