Was'Siim Ben Hssen Was'Siim Ben Hssen - 1 year ago 101
Twig Question

How can i access to array key in twig view symfony 3

I have a method that returns multiple arrays and I want to access to their data to manipulate them in

twig
:

repository

public function getmemberlist($list){
$memberId = explode('-',$list);
$membres = $this->getEntityManager()
->createQuery(
'SELECT m.nom, m.prenom, m.profile_pic
FROM MainBundle:Member m
WHERE m.id IN(:id)'
)
->setParameter('id',array_values($memberId))
->getResult();

return $membres;
}


controller

public function membreGroupeAction()
{
$em = $this->getDoctrine()->getManager();
$str = $em->getRepository("GroupGroupBundle:Groupe")->findOneBy(array('id'=>1));
$member = $em->getRepository("GroupGroupBundle:Groupe")->getmemberlist($str->getMembres());
var_dump($member);



return $this->render('@GroupGroup/layout/membres.html.twig',array("mem"=>$member));

}


this is the result of
var_dump($member)


var dump result

everything is good until now but i didn't find how to manipulate the data in my twig. I've tried this but it shows me an error

twig

{% extends '@GroupGroup/Group/groupe_mur_base.html.twig' %}
{% block panel %}
{% for i in mem %}
{% for j in i %}
{{ j[0].nom }}
{% endfor %}
{% endfor %}
<div class="jumbotron list-content" style="display: block;">
<ul class="list-group">
<li href="#" class="list-group-item title">
Liste des Membres
</li>
<li href="#" class="list-group-item text-left" id="listmembre" style="display: block;">
<div class="image">
<img class="img-thumbnail" src="http://bootdey.com/img/Content/User_for_snippets.png">
<span id="membername">Juan guillermo cuadrado</span>
<div><button id="btnajout" class="btn btn-primary">Ajouter</button></div>

</div>
<div class="break"></div>
</li>
<li href="#" class="list-group-item text-left" id="listmembre" style="display: block;">
<div class="image">
<img class="img-thumbnail" src="http://bootdey.com/img/Content/User_for_snippets.png">
<span id="membername">Juan guillermo cuadrado</span>
<div><button id="btnajout" class="btn btn-primary">Ajouter</button></div>

</div>
<div class="break"></div>
</li>


error

error

Answer Source

I'm not sure why you are using 2 for-loops in your code,

{% for i in  mem %} {# here you are looping members #}
     {% for j in i %} {# here you are looping attributes of the member #}
         {{ j }} {# output is bg, wass, http://.... #}
     {% endfor %}
 {% endfor %}

If you want to display the name of the members you should only use one loop

{% for i in  mem %}
<div>
    <img src="{{ i.profile_pic }}" alt="{{ i.nom }} {{ i.prenom }}" title="{{ i.nom }} {{ i.prenom }}" />
    {{ i.nom }} {{ i.prenom }}        
</div>
{% endfor %}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download