Related to entries in categories in a plugin Ok I can do this in twig but after searching high and low I can't find a good example of how to do it in a plugin.

I have a user profile field called interests which holds an array of what they are interested in. I want to pull out all entries that are related to the category id's held in the interests array. So in twig I do this:

{% for interest in userinterests %}
    {% for entry in entries.relatedTo(craft.categories(interest)) %}
        &lt;li&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/li&gt;
    {% endfor %}           
{% endfor %}


In my plugin function I currently have this but it is not returning anything in the array so I'm currently stuck:

$userinterests = craft()-&gt;users-&gt;getUserById($userid)-&gt;getContent()-&gt;getAttributes();
$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;relatedTo = array(
    'sourceElement' =&gt; $categories
);
$relatedEntries = craft()-&gt;elements-&gt;findElements($criteria);


I'm not sure what I am supposed to be adding to the relatedTo array but it also could be something else as I'm still trying to get my head around craft.

Any help much appreciated.
Ok worked this one out eventually myself. If anyone else finds this helpful.

$userinterests = craft()-&gt;users-&gt;getUserById($userid)-&gt;getContent()-&gt;getAttributes();
$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;relatedTo = array(
        'targetElement' =&gt; $userinterests['relevantFieldToCheckAgainst'],
        'field' =&gt; 'EntryFieldToCheckIn'
);
$relatedEntries = craft()-&gt;elements-&gt;findElements($criteria);