PHP SimpleXML按元素类型分组 [英] PHP SimpleXML Group By Element Type

查看:105
本文介绍了PHP SimpleXML按元素类型分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是我的困境。我正在寻找一种在PHP中使用 SimpleXML 来整合数据的方法。这就是我的意思。



假设我有一个如下所示的xml:

 < favorites> 
<有趣>
< type>电影< / type>
< character>詹姆斯·邦德< / character>
<名称>皇家赌场< /名称>
< /有趣>
<有趣>
< type>电影< / type>
< character> Jason Bourne< / character>
<名称> Bourne身份< /名称>
< /有趣>
<有趣>
< type> Book< / type>
< character> Lindsay Ford< / character>
<名称> Shantaram< / name>
< /有趣>
< / favorites>

现在就是我想要的样子:



电影


  • 皇家赌场 - 詹姆斯庞德伯恩

  • Bourne Identity - Jason Bourne



预订


  • Shantaram - Lindsay Ford


请帮助我!如果有任何事情是令人困惑的,请告诉我们。

/ p>


  • 从XML数据提取所有类型


    • 让它们为唯一,因为它们可能会出现多次(XPath可能有这种方法;我不知道它,所以我将使用 array_unique


  • 遍历这些类型,使用 @ Mark建议
    / b>



    类似于这可能会这样:

      $ xml = simplexml_load_string($ xmlData); 

    $ typesListXml = $ xml-> xpath('interesting / type');
    var_dump($ typesListXml); // var_dump#1

    if(!empty($ typesListXml)){
    $ typesList = array();
    foreach($ typesListXml as $ typeXml){
    $ typesList [] =(string)$ typeXml;
    }
    var_dump($ typesList); // var_dump#2

    $ typesList = array_unique($ typesList);
    var_dump($ typesList); // var_dump#3

    $ moviesForType = array();
    foreach($ typesList as $ type){
    $ rawData = $ xml-> xpath('interesting [type ='。$ type。'''');
    if(!empty($ rawData)){
    foreach($ rawData as $ rawMovie){
    $ moviesForType [$ type] [] = $ rawMovie->名称。 ' - '。 $ rawMovie-&G​​T;字符;
    }
    }
    }

    var_dump($ moviesForType); // var_dump#4

    //由您自己决定是否要用$ moviesForType ;-)

    }



    为了让事情更容易理解,下面是var_dump的输出:




    $ b

     数组
    0 =>> var_dump#1

    object(SimpleXMLElement)[2]
    string'Movie'(length = 5)
    1 =>
    object(SimpleXMLElement)[3]
    string'Movie'(length = 5)
    2 =>
    object(SimpleXMLElement)[4]
    字符串'Book'(length = 4)




    $ b

     数组
    0 =>> var_dump#2 字符串'Movie'(长度= 5)
    1 =>字符串'电影'(长度= 5)
    2 =>字符串'Book'(长度= 4)

    var_dump#3

     数组
    0 =>字符串'电影'(长度= 5)
    2 =>字符串'Book'(长度= 4)

    var_dump#3

      array 
    'Movie'=>
    数组
    0 =>字符串'皇家赌场 - 詹姆斯邦德'(长度= 26)
    1 =>字符串'Bourne Identity - Jason Bourne'(长度= 30)
    'Book'=>
    数组
    0 =>字符串'Shantaram - Lindsay Ford'(长度= 24)



    现在,您需要按照您的要求呈现这些数据;构建< ul> < li> 标签的双foreach循环可能会执行; - )

    Ok here's my dilemma. I am looking for a way to consolidate the data in a group using SimpleXML in PHP. Here's what I mean.

    Let's say I have an xml that looks like this:

      <favorites>
        <interesting>
            <type>Movie</type>
            <character>James Bond</character>
            <name>Casino Royale</name>
        </interesting>
        <interesting>
            <type>Movie</type>
            <character>Jason Bourne</character>
            <name>Bourne Identity</name>
        </interesting>
        <interesting>
            <type>Book</type>
            <character>Lindsay Ford</character>
            <name>Shantaram</name>
        </interesting>
      </favorites>
    

    Now here's what I want that to look like:

    Movie

    • Casino Royale - James Bond Bourne
    • Bourne Identity - Jason Bourne

    Book

    • Shantaram - Lindsay Ford

    Please help me!! Let me know if anything is confusing.

    解决方案

    If you don't know in advance the types of movies, you could :

    • extract all types from the XML data
      • get them as unique, as they may appear several times (There might be a way to do that with XPath ; I don't know it, so I'll use array_unique)
    • iterate over these types, using what @Mark proposed.


    Something like this would probably do :

    $xml = simplexml_load_string($xmlData);
    
    $typesListXml = $xml->xpath('interesting/type');
    var_dump($typesListXml);    // var_dump #1
    
    if (!empty($typesListXml)) {
        $typesList = array();
        foreach ($typesListXml as $typeXml) {
            $typesList[] = (string)$typeXml;
        }
        var_dump($typesList);    // var_dump #2
    
        $typesList = array_unique($typesList);
        var_dump($typesList); // var_dump #3
    
        $moviesForType = array();
        foreach ($typesList as $type) {
            $rawData = $xml->xpath('interesting[type="' . $type . '"]');
            if (!empty($rawData)) {
                foreach ($rawData as $rawMovie) {
                    $moviesForType[$type][] = $rawMovie->name . ' - ' . $rawMovie->character;
                }
            }
        }
    
        var_dump($moviesForType); // var_dump #4
    
        // Up to you to present $moviesForType the way you want ;-)
    
    }
    


    And to make things easier to understand, here are the var_dump's outputs :

    var_dump #1 :

    array
      0 => 
        object(SimpleXMLElement)[2]
          string 'Movie' (length=5)
      1 => 
        object(SimpleXMLElement)[3]
          string 'Movie' (length=5)
      2 => 
        object(SimpleXMLElement)[4]
          string 'Book' (length=4)
    

    var_dump #2 :

    array
      0 => string 'Movie' (length=5)
      1 => string 'Movie' (length=5)
      2 => string 'Book' (length=4)
    

    var_dump #3 :

    array
      0 => string 'Movie' (length=5)
      2 => string 'Book' (length=4)
    

    var_dump #3 :

    array
      'Movie' => 
        array
          0 => string 'Casino Royale - James Bond' (length=26)
          1 => string 'Bourne Identity - Jason Bourne' (length=30)
      'Book' => 
        array
          0 => string 'Shantaram - Lindsay Ford' (length=24)
    


    Now, it's up to you to present those data the way you want ; a double-foreach loop constructing <ul> and <li> tags would probably do ;-)

    这篇关于PHP SimpleXML按元素类型分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆