简化此 foreach 的最佳方法是什么? [英] What would be the best way to simplify this foreach?

查看:51
本文介绍了简化此 foreach 的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试一个 xml 对象以查看它的深度有多少层,结果发现它有 11 个元素的深度.我想知道我怎样才能做得更简单,以便将来我可以节省几分钟时间.

I was testing an xml object to see how many levels deep it went and found that it goes 11 elements deep. I'm wondering how I could have done this simpler so that in the future I can save a few minutes.

$xml = simplexml_load_file(drupal_get_path('module', 'my_module') . '/test_file.xml');
  dpm($xml);
  foreach($xml->section as $section_l1) {
    dpm('L1-------------------------------');
    foreach($section_l1->attributes() as $a => $b) {
      dpm($a . ' = ' . $b);
    }
    dpm('-----:');
    foreach($section_l1->section as $section_l2) {
      dpm('---L2--------------------------');
      foreach($section_l2->attributes() as $a => $b) {
        dpm($a . ' = ' . $b);
      }
      dpm('-----:');
      foreach($section_l2->section as $section_l3) {
        dpm('------L3---------------------');
        foreach($section_l3->attributes() as $a => $b) {
          dpm($a . ' = ' . $b);
        }
        dpm('-----:');
        foreach($section_l3->section as $section_l4) {
          dpm('------L4---------------------');
          foreach($section_l4->attributes() as $a => $b) {
            dpm($a . ' = ' . $b);
          }
          dpm('-----:');
          foreach($section_l4->section as $section_l5) {
            dpm('------L5---------------------');
            foreach($section_l5->attributes() as $a => $b) {
              dpm($a . ' = ' . $b);
            }
            dpm('-----:');
            foreach($section_l5->section as $section_l6) {
              dpm('------L6---------------------');
              foreach($section_l6->attributes() as $a => $b) {
                dpm($a . ' = ' . $b);
              }
              dpm('-----:');
              foreach($section_l6->section as $section_l7) {
                dpm('------L7---------------------');
                foreach($section_l7->attributes() as $a => $b) {
                  dpm($a . ' = ' . $b);
                }
                dpm('-----:');
                foreach($section_l7->section as $section_l8) {
                  dpm('------L8---------------------');
                  foreach($section_l8->attributes() as $a => $b) {
                    dpm($a . ' = ' . $b);
                  }
                  dpm('-----:');
                  foreach($section_l8->section as $section_l9) {
                    dpm('------L9---------------------');
                    foreach($section_l9->attributes() as $a => $b) {
                      dpm($a . ' = ' . $b);
                    }
                    dpm('-----:');
                    foreach($section_l9->section as $section_l10) {
                      dpm('------L10---------------------');
                      foreach($section_l10->attributes() as $a => $b) {
                        dpm($a . ' = ' . $b);
                      }
                      dpm('-----:');
                      foreach($section_l10->section as $section_l11) {
                        dpm('------L11---------------------');
                        foreach($section_l11->attributes() as $a => $b) {
                          dpm($a . ' = ' . $b);
                        }
                        dpm('-----:');
                        foreach($section_l11->section as $section_l12) {
                          dpm('------L12---------------------');
                          foreach($section_l12->attributes() as $a => $b) {
                            dpm($a . ' = ' . $b);
                          }
                          dpm('-----:');
                          foreach($section_l12->section as $section_l13) {
                            dpm('------L13---------------------');
                            foreach($section_l13->attributes() as $a => $b) {
                              dpm($a . ' = ' . $b);
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }

注意:drupal_get_path 和 dpm 是 Drupal CMS 函数,这里可以忽略.

Note: drupal_get_path and dpm are Drupal CMS functions and can be ignored here.

推荐答案

这应该有效.这是一个例子

function recurse($xml, $maxLevel = -1, $level = 0) {
    if ($maxLevel != -1 && $level > $maxLevel) {
        return;
    }

    if ($level == 0) {
        dpm($xml);
    }

    $string = '---------------------------------';
    $pos = $level * 3;
    $l = 'L'. ($level+1);
    $string = substr_replace($string, $l, $pos, strlen($l));
    foreach ($xml->section as $section) {
        dpm($string);
        foreach ($section->attributes() as $a=>$b) {
            dpm($a .' = '. $b);
        }
        dpm('-----:');
        recurse($section, $maxLevel, $level+1);
    }
}

$xml = simplexml_load_file(drupal_get_path('module', 'my_module') . '/test_file.xml');
recurse($xml);

您可以使用第二个参数指定要降低的最大级别,就像我在键盘示例中所做的那样.

You can specify the max level you want to go down using the second parameter, as I've done in the codepad example.

这篇关于简化此 foreach 的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆