php - Twig 中的二维数组

标签 php mysql arrays symfony twig

我将卫星图像存储在 mysql 数据库中。该表具有纬度、经度属性。我想将它们发送到 Twig 并显示为 map ,我的 php Controller 如下所示。

public function highlightAction()
{
    $highlighted=$this->getDoctrine()
        ->getRepository('AppBundle:satelliteImage')
        ->findAll();

    $images = array();
    foreach ($highlighted as $key => $high) {
        $images[$key] = base64_encode(stream_get_contents($high->getImage()));
    }

    return $this->render('satelliteImages/highlighted.html.twig',array(
        'highlighted' => $highlighted,
        'images' => $images

    ));

}

我的 Twig 代码是这样的:

    <tbody>
    {% for key,high in highlighted %}
        <tr>
            <img alt="Embedded Image" src="data:image/png;base64,{{ images[key] }}" />

        </tr>
    {% endfor %}
    </tbody>

我将图像显示为垂直阵列。任何建议,我可能需要将它们显示为 map 。 Twig 中的二维数组?

最佳答案

你可以这样使用:

<table>
    <tbody>
    {% for key in 0..2  %}
        <tr>
            {% for key in 0..3 %}
                <td>
                    <img alt="Embedded Image" src="data:image/png;base64,
                        {{ images[loop.parent.key*4 + key] }}" />
                </td>
            {% endfor %}
        </tr>
    {% endfor %}
    </tbody>
</table>

阅读Twig for documentation .

关于php - Twig 中的二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41518574/

相关文章:

php - 如何遍历数据集中的 "window"数据?

php - $.ajax 成功函数返回 null

php - 使用 php 将数据库转换为 CSV

mysql - 选择带有 IN 子句 : filling the blanks 的查询

mysql - SequelizeEagerLoadingError : product is not associated to collection

java - 格式化字符串数组以反转字符串

php - 为什么不能使用字符串数字作为 session 变量中的索引?

python - 使用 itertools 创建 numpy 数组

php - 按属性值减少 WooCommerce 商品库存

php - 替换wordpress数据库中的网站url