我真的希望有人可以在这里帮助我,自从这个问题的最后几天以来我一直困扰着。我有以下xml对象数组:
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
)
)
我有一个带有一些值的xml文件,我用简单的xml读取,然后用Car类创建了car对象。我试图解决这个问题几天了,如何显示这个数组中的数据,标题只出现一次像一个部分。见下面的例子。
SUVS
通用汽车1 美国 随机 22 000
通用汽车2 美国 随机 28 000
通用汽车3 美国 随机 29 000
拾取
通用卡车1 美国 随机 31 000
通用卡车2 美国 随机 39 000
答案 0 :(得分:1)
您的部分似乎基于XML中类型值的内容。
我有2个想法来解决这个问题。
第一个想法: 如果您有一个foreach循环来创建您的部分,您可以在打印汽车名称之前查看类型:
//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
}
}
第二个想法:修改您的阅读程序。
目前,您正在使用Car对象创建一个数组。创建Car对象时,应该已知车型。因此,您可以将不同类型的Car对象添加到不同的数组中。
这里有一些示例代码: 而不是像这样创建一个数组
$array[] = new Car()
你可以创建像这样的东西
$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;
然后你可以创建一个foreach循环来创建两个列表:
foreach($cars as $type => $carArray){
echo '<strong>'.$type.'</strong>';
foreach($carArray as $car){
//create your output for a Car object
}
}
或分别在每个列表上:
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
}.
代码片段只是用来说明这些想法的例子。