Set Value of Single Object in Multidimensional Array in Twig Template

How to set multi-dimensional array in Twig?

In Twig, arrays are marked with [], and hashes with {}. A hash is a key-value pair with explicit keys (strings or integers), an array is simply a set of values without any explicitly defined keys (they will be indexed numerically).

In order to use a hash, you MUST provide a key for each element.

So, what you want is probably {% set fields = [ {'name': 'description', 'value': '1'}, { 'name': 'abc', 'value': '2'}, { 'name':'tags', 'value': '3'} ] %}

Show a multidimensional array in twig

This is my solution.
Thanks to the help of DarkBee
I saw this solution https://stackoverflow.com/a/34402216/2400373 which adapts to my problem.
It presents other problems here I explain

My DQL is this:

$dql="SELECT c,o
FROM BackendBundle:Orders o
JOIN o.users u
JOIN BackendBundle:Customer c
WITH u.email = c.billEmail
where o.orderid='$var'";

After it's necessary to add a twig extension:

//src/AppBundle/Twig/AppExtension.php
<?php
// src/AppBundle/Twig/AppExtension.php
namespace AppBundle\Twig;

class AppExtension extends \Twig_Extension
{
public function getFilters()
{
return array(
new \Twig_SimpleFilter('cast_to_array', array($this, 'objectFilter')),
);
}

public function objectFilter($stdClassObject) {
// Just typecast it to an array
$response = (array)$stdClassObject;

return $response;
}
}

After in twig:

{% for key, value in ordenes|cast_to_array %}
<td id="col" class="hidden-xs">{{ value }}</td>
{% endfor %}

Other problems in my Entities I need add __toString ... for example:

public function __toString()
{
return (string)$this->getBillEmail();
}

With this already works. thank you help

how to display a specified value from an multidimensional array in twig template

You overwrite your array $data['service'] with every loop pass in the PHP. I'm not quite certain what you want to see, but I expect the twig will work if you change the PHP to this:

$data['service'] = array();

$sql= "SELECT * FROM services";

$serviceDetails= mysql_query($sql);

while($row = mysql_fetch_array($serviceDetails, MYSQL_ASSOC))
{

$data['service'][]=$row['service_name']

}

echo $twig->render('test.html',$data);

Multidimensional array Twig

Okay, you can do that like this: (if you know your array has only one more dimension, total 2 dimensions).

{% for key, item in items %}
{% if item is iterable %}
{% for sub_item in item %}
Do something...
{% endfor %}
{% else %}
Do something else...
{% endif %}
{% endfor %}

If you have a multidimensional array that has more than 1 sub array, you have to call a function recursively to reach that other sub dimensions. You can do it in your twig extension file.

A recursive function is calling itself to reach sub items in a multidimensional array. I don't know what do you want to do with that array but i'm gonna make a basic function depends on your array.

function recursiveTwig($array)
{
foreach($array as $key => $value){
if(is_array($key)) {
$this->recursiveTwig($key);
} else {
//Do something with your value...
}
}
}

Macro:

How to render a tree in Twig

How to pull a value out a or multi dimensional array using twig

Replace this :

    {% for key,value in value.images %}
<div class="col-md-3">
<img src="{{ value.url }}" alt="Sample Image" class="thumbnail img-responsive" />
</div>
{% endfor %}

by:

    <div class="col-md-3">
<img src="{{ value.images.low_resolution.url }}" alt="Sample Image" class="thumbnail img-responsive" />
</div>

Be sure not to use twice key and value variables for multiple loops

Based on another stackoverflow answer, here's how to navigate in an associative array in TWIG: https://stackoverflow.com/a/14199125/684101

How to check a multidimensional Twig array for values?

Set a flag and use a loop. Afterwards you can use the flag in if conditions.

{% set flag = 0 %}
{% for tag in tasks.tags %}
{% if tag.id == "20" %}
{% set flag = 1 %}
{% endif %}
{% endfor %}
{{ flag }}


Related Topics



Leave a reply



Submit