duanboniao5903 2015-10-20 16:38
浏览 24
已采纳

simplexml对象和彼此之间的数组

I have a simplexml script which creates a complex object, I just want the information from within the div 'grid' so grab I use xpath to get it.

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

This results in big object array which seems to be a mix of objects and arrays and im really struggling to traverse my way through it. The below is a very cut down version. There are 30 members 'Person 1' etc. Each person has a list which contains 25 items and its these I need to access/work on. (["li"]=> array(25))

Ideally I need to loop through each member, and then subsequently loop through each li item, but im getting hung up on using $variable['name'] vs $object->name

Just testing I have tried a variety of ways to get the persons name and I think Im confusing myself trying to wrap my head around objects traversal.

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"
        }
      }
  • 写回答

1条回答 默认 最新

  • dongmao4486 2015-10-21 11:32
    关注

    Let's use simplexml along with xpath, see comments inline below

    <?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
    */
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用