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 关于c++外部库文件宏的问题,求解
  • ¥15 office打开卡退(新电脑重装office系统后)
  • ¥300 FLUENT 火箭发动机燃烧EDC仿真
  • ¥15 【Hadoop 问题】Hadoop编译所遇问题hadoop-common: make failed with error code 2
  • ¥15 vb6.0+webbrowser无法加载某个网页求解
  • ¥15 RPA财务机器人采购付款流程
  • ¥15 计算机图形多边形及三次样条曲线绘制
  • ¥15 根据protues画的图用keil写程序
  • ¥200 如何使用postGis实现最短领规划?
  • ¥15 pyinstaller打包错误