<p>Your plugin should be able to handle PHP arrays just fine.</p>

<p>craft/plugins/myplugin/variables/MyPluginVariable.php</p>

<pre><code>function getArray()
{
    return array('hi', 'there');
}
</code></pre>

<p>craft/templates/template.twig</p>

<pre><code>{% set result = craft.myPlugin.getArray() %}
{% for item in result %}
    {{ item }}
{% endfor %}
</code></pre>

<p>The error you're seeing is likely because <code>output</code> itself is an array. If you have devMode enabled you can try a <code>{{ dump(output) }}</code> inside your for-loop to see what it actually is / contains.</p>

CC BY-SA 3.0
2017-10-17T13:40:50.340Z
i now only have this in twig  {% set result = craft.winkelwagen.printWinkelWagen(currentUser.id) %} But it still gives the error however if i use print_r in php it outputs fine.
Bob van Velzen
CC BY-SA 3.0
2017-10-18T08:30:37.120Z
Hang on - the only thing in your template is the 'set result = ...'-line - and you're still seeing a Twig-error about array to string conversion?

If that's the case I suspect that something in one of your other templates (that this one is extending or including) is throwing the error - because there's no way a simple variable declaration should throw that error.
Fuglsetrampen
CC BY-SA 3.0
2017-10-17T08:16:40.173Z
2017-10-17T11:04:41.797Z
2017-10-17T11:04:41.797Z
Fuglsetrampen
<p>I'am writing a plugin that has to return an array of data, however Twig does not seem to accept it if I return a PHP array.  I don't want to use an explode in Twig to make it into an array.</p>

<p>I have seen in other plugins i can just call a function from twig like this.</p>

<pre><code>{% set result = craft.winkelwagen.printWinkelWagen(currentUser.id) %}

{% for output in result %}
    {{ output }}
{% endfor %}
</code></pre>

<p>However, I get an array to string conversion error.  Does anyone know the correct way to do this?</p>

<p>Thanks in advance.</p>

<plugin-development><plugins><php><array>
Returning an array to Twig from a plugin
# Returning an array to Twig from a plugin

(Asked by: username_0 on 2017-10-17)

I'am writing a plugin that has to return an array of data, however Twig does not seem to accept it if I return a PHP array.  I don't want to use an explode in Twig to make it into an array.
I have seen in other plugins i can just call a function from twig like this.
{% set result = craft.winkelwagen.printWinkelWagen(currentUser.id) %}

{% for output in result %}
    {{ output }}
{% endfor %}

However, I get an array to string conversion error.  Does anyone know the correct way to do this?
Thanks in advance.

**Tags:** plugin-development, plugins, php, array


---

## 1 answers

### Answer by username_1 on 2017-10-17. Score: 4

Your plugin should be able to handle PHP arrays just fine.
craft/plugins/myplugin/variables/MyPluginVariable.php
function getArray()
{
    return array('hi', 'there');
}

craft/templates/template.twig
{% set result = craft.myPlugin.getArray() %}
{% for item in result %}
    {{ item }}
{% endfor %}

The error you're seeing is likely because output itself is an array. If you have devMode enabled you can try a {{ dump(output) }} inside your for-loop to see what it actually is / contains.

> **Comments:**
> **username_0**: i now only have this in twig  {% set result = craft.winkelwagen.printWinkelWagen(currentUser.id) %} But it still gives the error however if i use print_r in php it outputs fine.  
> **username_1**: Hang on - the only thing in your template is the 'set result = ...'-line - and you're still seeing a Twig-error about array to string conversion?  If that's the case I suspect that something in one of your other templates (that this one is extending or including) is throwing the error - because there's no way a simple variable declaration should throw that error.  

---

