dongxi7609 2014-05-13 16:44 采纳率: 0%
浏览 31

在数组的元素上创建嵌套的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?

  • 写回答

1条回答 默认 最新

  • dsdfd2322 2014-05-13 17:02
    关注

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程