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 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,