在数组的元素上创建嵌套的foreach

I need to create nested foreach loops between them as there are elements of an array.

example:

This is the array starting

print_r($container);



Array
(
    [list1] => Array
        (
            [0] => Array
                (
                    [id_topic] => 45
                    [id_topicGroup] => 44
                    [topic] => topic1 di 44
                )

            [1] => Array
                (
                    [id_topic] => 46
                    [id_topicGroup] => 44
                    [topic] => topic2 di 44
                )

            [2] => Array
                (
                    [id_topic] => 47
                    [id_topicGroup] => 44
                    [topic] => topic3 di 44
                )

        )

    [list2] => Array
        (
            [0] => Array
                (
                    [id_topic] => 48
                    [id_topicGroup] => 45
                    [topic] => topic1 di 45
                )

            [1] => Array
                (
                    [id_topic] => 49
                    [id_topicGroup] => 45
                    [topic] => topic2 di 45
                )

            [2] => Array
                (
                    [id_topic] => 50
                    [id_topicGroup] => 45
                    [topic] => topic3 di 45
                )

        )

)

This is the processing for printing the elements for this case

foreach($container)['list1'] as $el_lista1) {
                foreach($container)['list2'] as $el_lista2) {
                    echo $el_lista1 . " " . $el_lista2 . "<br>
";
                }                
            }

This is the output

topic1 di 44 topic1 di 45

topic1 di 44 topic2 di 45

topic1 di 44 topic3 di 45

topic2 di 44 topic1 di 45

topic2 di 44 topic2 di 45

topic2 di 44 topic3 di 45

topic3 di 44 topic1 di 45

topic3 di 44 topic2 di 45

topic3 di 44 topic3 di 45

But the problem is that it has to manage an unlimited number of lists and forums on the inside as I do?

I think I need to create a number of internal foreach loops among them according to the number of lists such as these. Can you do this?

you need to use RecursiveArrayIterator

<?php
$arr=Array
(
    "list1" => Array
        (
            "0" => Array
                (
                    "id_topic" => 45,
                    "id_topicGroup" => 44,
                    "topic" => "topic1 di 44"
                ),

            "1" => Array
                (
                    "id_topic" => 46,
                    "id_topicGroup" => 44,
                    "topic" => "topic2 di 44",
                ),
            "2" => Array
                (
                    "id_topic" => 47,
                    "id_topicGroup" => 44,
                    "topic" => "topic3 di 44",
                )

        ),

    "list2" => Array
        (
            "0" => Array
                (
                    "id_topic" => 48,
                    "id_topicGroup" => 45,
                    "topic" => "topic1 di 45"
                ),

            "1" => Array
                (
                    "id_topic" => 49,
                    "id_topicGroup" => 45,
                    "topic" => "topic2 di 45",
                ),

            "2" => Array
                (
                    "id_topic" => 50,
                    "id_topicGroup" => 45,
                    "topic" => "topic3 di 45"
                )

        )

);

$iterator = new RecursiveArrayIterator($arr); 
iterator_apply($iterator, 'traverseStructure', array($iterator)); 

// from php manual

function traverseStructure($iterator) { 

    while ( $iterator -> valid() ) { 

        if ( $iterator -> hasChildren() ) { 

            traverseStructure($iterator -> getChildren()); 

        } 
        else { 
            echo $iterator -> key() . ' : ' . $iterator -> current() .'<br>';    
        } 

        $iterator -> next(); 
    } 
} 

//print according to your need

output:

id_topic : 45
id_topicGroup : 44
topic : topic1 di 44
id_topic : 46
id_topicGroup : 44
topic : topic2 di 44
id_topic : 47
id_topicGroup : 44
topic : topic3 di 44
id_topic : 48
id_topicGroup : 45
topic : topic1 di 45
id_topic : 49
id_topicGroup : 45
topic : topic2 di 45
id_topic : 50
id_topicGroup : 45
topic : topic3 di 45