且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在php中的xml标记数组中获取内部文本

更新时间: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>';
?>