1

xml オブジェクトをテストして、それが何レベルの深さになるかを調べたところ、11 要素の深さであることがわかりました。将来的に数分節約できるように、どうすればこれをもっと簡単にできたのでしょうか。

$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 関数であり、ここでは無視できます。

4

4 に答える 4

1

再帰関数を使用できます:

$xml = simplexml_load_file(drupal_get_path('module', 'my_module') . '/test_file.xml');
dpm($xml);
traverse($xml, 1);

private function traverse($section, $level) {
  dpm('L'.$level.'-------------------------------');

  foreach($section->attributes() as $a => $b) {
    dpm($a . ' = ' . $b);
  }

  dpm('-----:');

  traverse($section->section, $level+1)
}
于 2013-11-06T15:30:08.570 に答える
1

各レベルで同様のことを行っているため、このロジックを関数に入れ、各レベルでそのロジックを呼び出すことができます。(テストされていない)例は次のようになります。

$xml = simplexml_load_file(drupal_get_path('module', 'my_module') . '/test_file.xml');
function processXML($xml, $level = 1){
    dpm($xml);
    foreach($xml->section as $section_l1) {
         dpm('L'.$level.'-------------------------------');
         foreach($section_l1->attributes() as $a => $b) {
            dpm($a . ' = ' . $b);
         }
         dpm('-----:');
         processXML($section_l1,$level+1);
    }
}
于 2013-11-06T15:29:07.457 に答える
0

再帰関数が必要です:

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

$depth = $xml->section; // The array you want to find the depth
$iterator = 0; // The starting level
recurse_count($depth, $iterator); // Fire the function

function recurse_count($array, $iterator) {
    foreach($array->section as $children) {
        $iterator++; // Increment the iterator
        dpm('---L'.$iterator.'--------------------------');
        foreach($children->attributes() as $a => $b) {
            dpm($a . ' = ' . $b);
        }
        dpm('-----:');
    }
    recurse_count($children, $iterator); // Fire the function again
}

再帰関数は、それ自体を呼び出す関数であり、問​​題に適しています。この例は、問題に対して機能するはずです。

于 2013-11-06T15:28:41.857 に答える