更新时间:2023-09-14 11:21:04
你可以试试
$list = groupBy($xml, "WebCategory");
foreach ( $list['Mobiles'] as $product ) {
printf('<li>%s %s</li>', $product->Code, $product->Name);
}
输出
使用的函数
function groupBy($xml, $categoryName) {
$xml = new \SimpleXMLElement($xml);
$category = array();
foreach ( $xml as $row ) {
$attr = $row->attributes();
if (! isset($attr->$categoryName)) {
trigger_error("$categoryName does not exist in XML");
break;
}
$category[(string) $attr->$categoryName][] = $attr;
}
return $category;
}