<?php
$xmlstr = '<?xml version="1.0"?>
<catalog>
<car>
<title>This is car</title>
<price>44.95</price>
<model>2018</model>
<description>An in-depth look at creating applications with XML.</description>
</car>
<bike>
<title>this is bike</title>
<price>33.58</price>
<description>Just the dummy description</description>
</bike>
<wheels>
<title>this is wheel</title>
<price>33.58</price>
<description>Just the dummy description</description>
</wheels>
<bike>
<title>this is bike</title>
<price>33.58</price>
<description>Just the dummy description</description>
</bike>
</catalog>';
$xml = simplexml_load_string($xmlstr);
foreach ($xml->children() as $product) {
if ($product->getName() == 'car') {
carFunc($product->title, $product->price, $product->model, $product->description);
}
if ($product->getName() == 'bike') {
bikeFunc($product->title, $product->price, $product->description);
}
if ($product->getName() == 'wheels') {
wheelFunc($product->title, $product->price, $product->description);
}
}
function carFunc($title, $price, $model, $description) {
echo "Car: $title: It's a $model selling for $price. In detail: $description\n";
}
function bikeFunc($title, $price, $description) {
echo "Bike: $title: It sells for $price. In detail: $description\n";
}
function wheelFunc($title, $price, $description) {
echo "Wheel: $title: It sells for $price. In detail: $description\n";
}
preferences:
34.46 ms | 402 KiB | 5 Q