2015-10-20 25 views
0

我有一個簡單的xml腳本,它創建了一個複雜的對象,我只想從div'grid'中獲取信息,所以抓住我使用xpath來獲取它。simplexml對象和對方陣列

$id = $sxml->xpath("//*[@id='grid_data']"); 

這會導致大的對象數組,這似乎是對象和數組的混合,我真的很努力地穿過它。下面是一個非常簡化的版本。有30個成員'人1'等。每個人都有一個列表,其中包含25個項目,我需要訪問/工作。 ([「li」] => array(25))

理想情況下,我需要遍歷每個成員,然後循環遍歷每個li項目,但即時通訊使用$ variable ['name'] vs $ object-> name

只是測試我嘗試了各種方法來獲取人名,我想我困惑着自己試圖圍繞對象遍歷我的頭。

echo $id[0]->div[0][p][a]; 
echo $id[0]['div'][0]['p']['a']; 
echo $id->0->div 

array(1) { 
    [0]=> 
    object(SimpleXMLElement)#3 (2) { 
    ["@attributes"]=> 
    array(1) { 
     ["id"]=> 
     string(9) "grid_data" 
    } 
    ["div"]=> 
    array(35) { 
     [0]=> 
     object(SimpleXMLElement)#4 (4) { 
     ["@attributes"]=> 
     array(1) { 
      ["class"]=> 
      string(9) "stff_grid" 
     } 
     ["p"]=> 
     object(SimpleXMLElement)#39 (2) { 
      ["@attributes"]=> 
      array(2) { 
      ["class"]=> 
      string(16) "staff" 
      ["id"]=> 
      string(15) "1328" 
      } 
      ["a"]=> 
      string(17) "Person 1" 
     } 
     ["ul"]=> 
     object(SimpleXMLElement)#40 (2) { 
      ["@attributes"]=> 
      array(1) { 
      ["class"]=> 
      string(0) "" 
      } 
      ["li"]=> 
      array(25) { 
      [0]=> 
      object(SimpleXMLElement)#42 (2) { 
       ["@attributes"]=> 
       array(1) { 
       ["class"]=> 
       string(16) "lrge" 
       } 
       ["a"]=> 
       string(2) "00" 
      } 
      [1]=> 
      object(SimpleXMLElement)#43 (2) { 
       ["@attributes"]=> 
       array(1) { 
       ["class"]=> 
       string(16) "lrge" 
       } 
       ["a"]=> 
       string(2) "01" 
      } 
      [2]=> 
      object(SimpleXMLElement)#44 (2) { 
       ["@attributes"]=> 
       array(1) { 
       ["class"]=> 
       string(16) "lrge" 
       } 
       ["a"]=> 
       string(2) "02" 
      } 
      } 
     } 
     ["div"]=> 
     object(SimpleXMLElement)#41 (1) { 
      ["@attributes"]=> 
      array(1) { 
      ["class"]=> 
      string(10) "left" 
      } 
     } 
     } 
     [1]=> 
     object(SimpleXMLElement)#5 (4) { 
     ["@attributes"]=> 
     array(1) { 
      ["class"]=> 
      string(9) "stff_grid" 
     } 
     ["p"]=> 
     object(SimpleXMLElement)#41 (2) { 
      ["@attributes"]=> 
      array(2) { 
      ["class"]=> 
      string(16) "staff" 
      ["id"]=> 
      string(15) "no_1333" 
      } 
      ["a"]=> 
      string(11) "Person 2" 
     } 
     ["ul"]=> 
     object(SimpleXMLElement)#40 (2) { 
      ["@attributes"]=> 
      array(1) { 
      ["class"]=> 
      string(0) "" 
      } 
      ["li"]=> 
      array(25) { 
      [0]=> 
      object(SimpleXMLElement)#66 (2) { 
       ["@attributes"]=> 
       array(1) { 
       ["class"]=> 
       string(16) "lrge" 
       } 
       ["a"]=> 
       string(2) "00" 
      } 
      [1]=> 
      object(SimpleXMLElement)#65 (2) { 
       ["@attributes"]=> 
       array(1) { 
       ["class"]=> 
       string(16) "lrge" 
       } 
       ["a"]=> 
       string(2) "01" 
      } 
      [2]=> 
      object(SimpleXMLElement)#64 (2) { 
       ["@attributes"]=> 
       array(1) { 
       ["class"]=> 
       string(16) "lrge" 
       } 
       ["a"]=> 
       string(2) "02" 
      } 
      } 
     } 
     ["div"]=> 
     object(SimpleXMLElement)#39 (1) { 
      ["@attributes"]=> 
      array(1) { 
      ["class"]=> 
      string(10) "left" 
      } 
     } 
     } 
     [2]=> 
     object(SimpleXMLElement)#6 (1) { 
     ["@attributes"]=> 
     array(1) { 
      ["class"]=> 
      string(6) "spacer" 
     } 
     } 
+0

意見能否請你地方,比如引擎收錄後它的實際的XML或一部分,我想測試之前,我建議的事情。 –

+0

好的,我會看看我是否可以將所有個人信息從團隊成員系統中刪除,以便頁面包含更多內容。 –

+0

這將是偉大的,如果你需要從XML過濾數據只提供一個樣本與少數節點 –

回答

1

讓我們用simplexmlxpath一起,看在線低於

<?php 
$xml = simplexml_load_file('xml.xml'); 

// let's take all the divs that have the class "stff_grid" 
$divs = $xml->xpath("//*[@class='stff_grid']"); 

// for each of these elements, let's print out the value inside the first p tag 
foreach($divs as $div){ 
    print $div->p->a . PHP_EOL; 

    // now for each li tag let's print out the contents inside the a tag 
    foreach ($div->ul->li as $row){ 
     print " - " . $row->a . PHP_EOL; 
    } 
} 
/* this outputs the following 
Person 1 
    - 1 hr 
    - 2 hr 
    - 3 hr 
    - 4 hr 
    - 5 hr 
    - 6 hr 
    - 7 hr 
    - 8 hr 
Person 2 
    - 1 hr 
    - 2 hr 
    - 3 hr 
    - 4 hr 
    - 5 hr 
    - 6 hr 
    - 7 hr 
    - 8 hr 
Person 3 
    - 1 hr 
    - 2 hr 
    - 3 hr 
    - 4 hr 
    - 5 hr 
    - 6 hr 
    - 7 hr 
    - 8 hr 
*/ 
+0

你的明星。我的問題是抓住父div,沒有意識到我可以加載每個stff_grid到同一個對象。這很強大。我在其他地方有一個相關的問題,試圖從一個元素grrr中獲得這個類。但我更近一步: - D –

+0

不用客氣@PaulM –