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

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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?