1

I have a problem with an error when I print in twig file the value of a doctrine query..

In my controller I have this code in a for loop, to get more elements from my database:

$pyramid[$x]['id_user']   = $queryconteggio;

And if I print with dump function I receive more array for 1 user now:

array(2) { [0]=> array(1) { [1]=> string(2) "94" } [1]=> array(1) { [1]=> string(3) "103" } }

And now for another user:

array(1) { [0]=> array(1) { [1]=> string(3) "101" } }

The values are accurate but when I print without dump:

{{ pyramid.id_user }}

It gives me this error:

An exception has been thrown during the rendering of a template ("Notice: Array to string conversion") in DtEcBundle:Profilo:digitalpr-profile.html.twig at line 53.

At the line 53 there is this code in a for: {{ pyramid.id_user }}

How can I print the value of my arrays without error?

1 Answer 1

2

pyramid.id_user is not a string but rather an array of arrays of strings, all with key "1". To print it you need to do something like:

{% for id in pyramid.id_user %}
    {{ id[1] }}
{% endfor %}
Sign up to request clarification or add additional context in comments.

5 Comments

Looking more into detail into the data samples I see that they are not arrays of strings, but rather arrays of arrays of string and in the second array the key always seems to be "1" (no idea why). So you could try {{ id[1] }}
To complete Carlos's answer, do you have something if you try: {% for key, id in pyramid.id_user %} {{ key }}: {{ id|e }}{% endfor %}
in dump: array(1) { [0]=> array(1) { [1]=> string(3) "101" } } array(0) { } ... and with your code this: An exception has been thrown during the rendering of a template ("Notice: Array to string conversion") in DtEcBundle:Profilo:digitalpr-profile.html.twig at line 46 @scoolnico
Sorry, not sure I understand
Yeeeeeeeah, work with id[1], sorry for my english and thanks everybody for the answers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.