dounei9043 2016-10-30 01:10
浏览 32
已采纳

简单的Xml对象数组按节显示

I really hope someone can help me here, I'm stuck since the last few days on this issue. I have the following array of xml objects:

Array
(
[0] => Car Object
    (
        [type:Car:private] => SimpleXMLElement Object
            (
                [0] => SUVS
            )

        [name:Car:private] => SimpleXMLElement Object
            (
                [0] => Generic Car 1
            )

        [origin:Car:private] => SimpleXMLElement Object
            (
                [0] => USA
            )

        [description:Car:private] => SimpleXMLElement Object
            (
                [0] => Random
            )

        [price:Car:private] => SimpleXMLElement Object
            (
                [0] => 22 000
            )

    )

[1] => Car Object
    (
        [type:Car:private] => SimpleXMLElement Object
            (
                [0] => SUVS
            )

        [name:Car:private] => SimpleXMLElement Object
            (
                [0] => Generic Car 2
            )

        [origin:Car:private] => SimpleXMLElement Object
            (
                [0] => USA
            )

        [description:Car:private] => SimpleXMLElement Object
            (
                [0] => Random
            )

        [price:Car:private] => SimpleXMLElement Object
            (
                [0] => 28 000
            )

    )

[2] => Car Object
    (
        [type:Car:private] => SimpleXMLElement Object
            (
                [0] => SUVS
            )

        [name:Car:private] => SimpleXMLElement Object
            (
                [0] => Generic Car 3
            )

        [origin:Car:private] => SimpleXMLElement Object
            (
                [0] => USA
            )

        [description:Car:private] => SimpleXMLElement Object
            (
                [0] => Random
            )

        [price:Car:private] => SimpleXMLElement Object
            (
                [0] => 29 000
            )

    )

[3] => Car Object
    (
        [type:Car:private] => SimpleXMLElement Object
            (
                [0] => Pickup
            )

        [name:Car:private] => SimpleXMLElement Object
            (
                [0] => Generic Truck 1
            )

        [origin:Car:private] => SimpleXMLElement Object
            (
                [0] => USA
            )

        [description:Car:private] => SimpleXMLElement Object
            (
                [0] => Random
            )

        [price:Car:private] => SimpleXMLElement Object
            (
                [0] => 31 000
            )

    )

    [4] => Car Object
    (
        [type:Car:private] => SimpleXMLElement Object
            (
                [0] => Pickup
            )

        [name:Car:private] => SimpleXMLElement Object
            (
                [0] => Generic Truck 2
            )

        [origin:Car:private] => SimpleXMLElement Object
            (
                [0] => USA
            )

        [description:Car:private] => SimpleXMLElement Object
            (
                [0] => Random
            )

        [price:Car:private] => SimpleXMLElement Object
            (
                [0] => 39 000
            )

    )

I had an xml file with some values that I read with simple xml and then I created car objects with my Car class. Im trying to figure this out for a few days now, How can I display the data from the this array with the title only appearing once like a section. See example below.

SUVS

Generic Car 1 USA Random 22 000

Generic Car 2 USA Random 28 000

Generic Car 3 USA Random 29 000

Pickup

Generic Truck 1 USA Random 31 000

Generic Truck 2 USA Random 39 000

  • 写回答

1条回答 默认 最新

  • drhdjp97757 2016-10-30 14:21
    关注

    It seems your sections are based on the content of the type value from your XML.

    I have 2 ideas to solve this.

    First idea: If you have a foreach loop to create your sections, you can take a look at the type before you print the car's name:

    //example code
    
    echo '<strong>SUVS</strong>';
    foreach($cars as $car){
        if($car->getType() == 'SUVS'){
            //create your output for a Car object
        }
    }
    
    echo '<strong>Pickups</strong>';
    foreach($cars as $car){
        if($car->getType() == 'Pickup'){
            //create your output for a Car object
        }
    }
    

    Second idea: Modify your reading routine.

    Currently, you're creating one array with Car objects. While a Car object is created the car type should be already known. So, you could add Car objects from different type to different arrays.

    Here some example code: Instead of creating an array like this

    $array[] = new Car()
    

    you could create something like this

    $car = new Car();
    $type = $car->getType(); // returns 'Pickup' or 'SUVS'
    //perhaps it is necessary to convert SimpleXMLElement objects to a string:
    // $string = (string)$simplexmlelement;
    $array[$type][] = $car;
    

    Then you could make a foreach loop to create both lists:

    foreach($cars as $type => $carArray){
        echo '<strong>'.$type.'</strong>';
        foreach($carArray as $car){
            //create your output for a Car object
        }
    }
    

    or on each list separately:

    echo '<strong>Pickups</strong>';
    foreach($cars['Pickup'] as $car){
        //create your output for a Car object
    }
    
    //Some other code
    
    echo '<strong>SUVS</strong>';
    foreach($cars['SUVS'] as $car){
        //create your output for a Car object
    }.
    

    The code snippets are only examples to illustrate the ideas.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊