I have an array like this:
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
and I want to print it in a readble way like this:
Array
(
[a] => apple
[b] => banana
[c] => Array
(
[0] => x
[1] => y
[2] => z
)
)
I have to pass the array to a Twig template. I tried this, but I don’t get the result I want. Can you please advise me what do change?
In controller:
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
$b = json_encode($a);
$c = json_decode($b, true);
return $this->render('AcmeTestBundle:Home:data.html.twig', array('data' => $c));
In template:
{% extends '::base.html.twig' %}
{% block stylesheets %}
<link href="{{ asset('bundles/acmetest/css/test.css') }}" type="text/css" rel="stylesheet" />
{% endblock %}
{% block body %}
{{ data }}
{% endblock %}
(this is base.html.twig: )
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>{% block title %}Welcome!{% endblock %}</title>
{% block stylesheets %}{% endblock %}
<link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
</head>
<body>
{% block body %}{% endblock %}
{% block javascripts %}{% endblock %}
</body>
</html>
Try with the Twig Debug Extensions:
Twig 1.x
Twig 2.x
The debug/dump tags only work when the
debugenvironment option is set totrue.https://twig.symfony.com/doc/2.x/functions/dump.html
Also, you can try this without the debug extension:
Inside your template: