dougu9895 2018-01-03 10:59
浏览 35
已采纳

PHP foreach在foreach循环中转向Twig

I use the following code for foreach in PHP:

$fruit = array();

$fruit[] = array('id' => 1, 'name' => 'Banana 1');
$fruit[] = array('id' => 1, 'name' => 'Pear 1');
$fruit[] = array('id' => 1, 'name' => 'Mango 1');

$fruit[] = array('id' => 2, 'name' => 'Banana 2');
$fruit[] = array('id' => 2, 'name' => 'Pear 2');
$fruit[] = array('id' => 2, 'name' => 'Mango 2');

function get_pieces($id)
{
    $pieces = array();

    switch ($id)
    {
        case 1:
            $pieces[] = array('number' => 1);
            $pieces[] = array('number' => 2);
        break;

        case 2:
            $pieces[] = array('number' => 3);
            $pieces[] = array('number' => 4, 'qwerty' => 1);        
        break;
    }

    return $pieces;
}

foreach ($fruit as $item)
{
    echo '<p>';
    echo '<b>' . $item['name'] . '</b>';

    $pieces = get_pieces($item['id']);

    foreach ($pieces as $piece)
    {
        echo '<p>';
        echo '<i>' . $piece['number'] . '</i>';

        if (isset($piece['qwerty']))
        {
            echo ' => <i>qwerty is on</i>';
        }

        echo '</p>';
    }

    echo '</p>';
}

I want to get this into a Twig template. For the fruit array only, I have no problems, but the pieces part, gives only the results of case 2 in Twig. Here you see the current code I use:

$render = array();

$render = array_merge($render, array('fruit' => $fruit));
$render = array_merge($render, array('pieces' => get_pieces(1)));

$render = array_merge($render, array('fruit' => $fruit));
$render = array_merge($render, array('pieces' => get_pieces(2)));

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

{% for f in fruit %}
    <p>{{ f.id }}</p>
    <p>{{ f.name }}</p>
    {% for p in pieces %}
        <p>Piece {{ p.number }}</p>
    {% endfor %}
{% endfor %}

Can someone help me, to get this second pieces array also correct working in Twig?

  • 写回答

1条回答 默认 最新

  • douzi0609 2018-01-03 13:39
    关注

    Try using array_merge_recursive when merging the pieces array, as array_merge will override the keys from the previous array.

    The same keys are used within both cases, i.e 0 and 1. array_merge_recursive creates new keys.

    https://3v4l.org/RBZDo


    Updated

    Attach the pieces to each piece of fruit instead, and pull that out of the array.

    $fruit = array();
    
    $fruit[] = array('id' => 1, 'name' => 'Banana 1', 'pieces' => get_pieces(1));
    $fruit[] = array('id' => 1, 'name' => 'Pear 1', 'pieces' => get_pieces(1));
    $fruit[] = array('id' => 1, 'name' => 'Mango 1', 'pieces' => get_pieces(1));
    
    $fruit[] = array('id' => 2, 'name' => 'Banana 2', 'pieces' => get_pieces(2));
    $fruit[] = array('id' => 2, 'name' => 'Pear 2', 'pieces' => get_pieces(2));
    $fruit[] = array('id' => 2, 'name' => 'Mango 2', 'pieces' => get_pieces(2));
    
    // ...
    
    echo $twig->render('test.html', array('fruit' => $fruit));
    
    {% for f in fruit %}
        <p>{{ f.id }}</p>
        <p>{{ f.name }}</p>
        {% for p in f.pieces %}
            <p>Piece {{ p.number }}</p>
        {% endfor %}
    {% endfor %}
    

    Results:

    PHP: https://3v4l.org/kUKff
    Twig: https://twigfiddle.com/p2hqsr

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,