2014-02-11 59 views
0

這只是殺了我。如何循環處理SimpleXMLElement對象數組並返回節點默認值?

我想獲得每個字段的默認值,但我只是無法弄清楚如何循環槽的所有對象。嘗試用json_decode將它們轉換爲一個簡單的數組,但它不清楚要循環什麼。

請幫我看看這裏。任何幫助表示讚賞!

這就是:

SimpleXMLElement Object 
(
    [@attributes] => Array 
     (
      [name] => params 
     ) 

    [fieldset] => Array 
     (
      [0] => SimpleXMLElement Object 
       (
        [@attributes] => Array 
         (
          [name] => Cat1 
         ) 

        [field] => SimpleXMLElement Object 
         (
          [@attributes] => Array 
           (
            [name] => 
            [type] => list 
            [default] => 1 
           ) 

         ) 

       ) 

      [1] => SimpleXMLElement Object 
       (
        [@attributes] => Array 
         (
          [name] => Item2 
         ) 

        [field] => Array 
         (
          [0] => SimpleXMLElement Object 
           (
            [@attributes] => Array 
             (
              [name] => post1 
              [type] => text 
              [default] => 5 
             ) 

           ) 

          [1] => SimpleXMLElement Object 
           (
            [@attributes] => Array 
             (
              [name] => post2 
              [type] => text 
              [default] => 18 
             ) 


           ) 

          [2] => SimpleXMLElement Object 
           (
            [@attributes] => Array 
             (
              [name] => post3 
              [type] => text 
              [default] => 15 
             ) 

            [option] => Array 
             (
              [0] => Blue 
              [1] => Green 
             ) 

           ) 

         ) 

       ) 

         [2] => SimpleXMLElement Object 
       (
        [@attributes] => Array 
         (
          [name] => Cat2 
         ) 

        [field] => Array 
         (
          [0] => SimpleXMLElement Object 
           (
            [@attributes] => Array 
             (
              [name] => post6 
              [type] => text 
              [default] => 3 
             ) 

           ) 

          [1] => SimpleXMLElement Object 
           (
            [@attributes] => Array 
             (
              [name] => post7 
              [type] => text 
              [default] => 36 
             ) 


           ) 

          [2] => SimpleXMLElement Object 
           (
            [@attributes] => Array 
             (
              [name] => post7 
              [type] => text 
              [default] => 88 
             ) 


           ) 

         ) 

       ) 
) 

回答

1

試試這個(其中$xml是你的根對象)

foreach($xml->fieldset as $fieldset) { 
    foreach($fieldset->field as $field) echo (string)$field['default']; 
} 
+0

釘它!是的,謝謝 ! – Benn