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
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.