更新时间:2023-02-19 16:59:27
<?php
$xml = '<cars>
<brand name="Audi">
<model>A1</model>
<model>A3</model>
<model>A5</model>
</brand>
<brand name="Ferrari">
<model>F12</model>
<model>FF</model>
</brand>
</cars>';
$doc = simplexml_load_string($xml);
foreach ($doc->children() as $brand) {
foreach ($brand->children() as $model) {
$cars[(string)$brand->attributes()->name][] = (string)$model;
}
}
echo '<pre>';
print_r($cars);
echo '</pre>';
?>