$arr=[
0=>[
'name'=>'张三',
'site'=>'http://www.hu-rong.com',
],
1=>[
'name'=>'李四',
'site'=>'www.hu-rong.com',
],
];
$dom=new DOMDocument('1.0','utf-8');
$list=$dom->createElement('list');
foreach($arr as $k=>$v){
$item=$dom->createElement('item');
$index=$dom->createAttribute('index');
$index->value=$k;
$item->appendChild($index);
$name=$dom->createElement('name');
$name->appendChild($dom->createCDATASection($v['name']));
$site=$dom->createElement('site',$v['site']);
$item->appendChild($name);
$item->appendChild($site);
$list->appendChild($item);
}
$dom->appendChild($list);
$dom->save('test.xml');
生成效果:
http://www.hu-rong.com
www.hu-rong.com