0

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 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Have you clicked the link? The pieces part contains 4 elements: 3v4l.org/RBZDo
What are you trying to achieve? Only show the pieces part for the given fruit?
Ah ok, you're better attach the pieces to the fruit array, then pull that out with twig. I'll update my answer shortly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.