Problem with array and Twig symfony 2

1

I have a problem between the controller of an application and a template twig, what I do is to convert a field from one row of the database to UTF8 , and then send an array to the view and that field I get well formatted.

Driver and twig code:

foreach ($articles as $key):
        foreach ($key as $a => $b):
            $articles1[] = array($a,utf8_decode($b));
        endforeach;
    endforeach;

article is an array that returns a query to the database and which contains the following:

$articles1 = array(4) {
         [0]=> array(2) { [0]=> string(6) "titulo" [1]=> string(43) "texto1" }
         [1]=> array(2) { [0]=> string(3) "url" [1]=> string(43) "example-url" }
         [2]=> array(2) { [0]=> string(6) "titulo" [1]=> string(43) "texto2" }
         [3]=> array(2) { [0]=> string(3) "url" [1]=> string(43) "example-url2" }
}

The problem occurs when I pass this array to the view:

return $this->render('bundlecualquiera:articles:slide.html.twig', **array('articles1'=>$articles1));

This is the view where I apparently go through the array and only take the title as an example:

**{% for user in articles1 %}
   {{user.titulo}} 
 {% endfor %}****

The error it shows is: Key "title" for array with keys "0, 1" does not exist in .. (template).

It should look like this:

$articulos = array(2) {
            [0]=> array(3) {
                 ["url"]=> string(16) "url-exmple"
                 ["titulo"]=> string(47) "titulo de articulo" 
                 ["imagen"]=> string(23) "imagen.jpg" } 
            [1]=> array(3) {
                 ["url"]=> string(19) "url-example 2"
                 ["titulo"]=> string(39) "titulo de artiulo"
                 ["imagen"]=> string(20) "imagen.jpg" }
         }

Once the conversion to UTF8 has been made. But it remains as shown above ..:

$articles1 = array(4) {
         [0]=> array(2) { [0]=> string(6) "titulo" [1]=> string(43) "texto1" }
         [1]=> array(2) { [0]=> string(3) "url" [1]=> string(43) "example-url" }
         [2]=> array(2) { [0]=> string(6) "titulo" [1]=> string(43) "texto2" }
         [3]=> array(2) { [0]=> string(3) "url" [1]=> string(43) "example-url2" }
}

How can I make the array that returns me this way?

I would really appreciate it because I do not see how to do it.

Thanks

    
asked by otacon070 24.09.2016 в 22:45
source

1 answer

2

At each iteration of this loop:

foreach ($key as $a => $b):
    $articles1[] = array($a,utf8_decode($b));
endforeach;

You are generating an index in $ articles1

Use:

foreach ($articles as $key):
    $tArray = array();
    foreach ($key as $a => $b):
        $tArray [$a] = utf8_decode($b);
    endforeach;
    $articles1[] = $tArray;
endforeach;

To get the structure you need.

    
answered by 30.09.2016 / 10:04
source