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 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题