我需要理清以下XML(的foreach ProgramList)的孩子MajorDescription如何使用PHP SimpleDOM對XML子節點值進行排序?基於它的價值(或任何其他方法)
<ArrayOfProgramList xmlns="http://schemas.datacontract.org/2004/07/Taca.Resources" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ProgramList>
<AreaOfInterests xmlns:a="http://schemas.datacontract.org/2004/07/Taca">
<a:AreaOfInterest>
<a:Interest>ABORIGINAL STUDIES</a:Interest>
</a:AreaOfInterest>
</AreaOfInterests>
<Coop>true</Coop>
<MajorDescription>ABORIGINAL COMMUNITY AND SOCIAL DEVELOPMENT</MajorDescription>
<Program>ACSD</Program>
<ProgramLocations>
<ProgramLocation>
<Campus>Barrie</Campus>
</ProgramLocation>
</ProgramLocations>
<Term>201210</Term>
</ProgramList>
<ProgramList>
<AreaOfInterests xmlns:a="http://schemas.datacontract.org/2004/07/Taca">
<a:AreaOfInterest>
<a:Interest>GRADUATE CERTIFICATE STUDIES</a:Interest>
</a:AreaOfInterest>
<a:AreaOfInterest>
<a:Interest>HEALTH AND WELLNESS STUDIES</a:Interest>
</a:AreaOfInterest>
</AreaOfInterests>
<Coop>false</Coop>
<MajorDescription>ADVANCED CARE PARAMEDIC</MajorDescription>
<Program>PARM</Program>
<ProgramLocations>
<ProgramLocation>
<Campus>Barrie</Campus>
</ProgramLocation>
</ProgramLocations>
<Term>201210</Term>
</ProgramList>
</ArrayOfProgramList>
我試圖用SimpleDOM做到這一點,因爲我讀過的多數民衆贊成在其他SO問題上對XML進行排序的最簡單方法。
我已經嘗試使用:
foreach($listxml->sortedXPath('//ArrayOfProgramList/ProgramList','//ArrayOfProgramList/ProgramList/MajorDescription') as $program){ ... }
和其他各種類似的 '排序' 值,如 '@MajorDescription', '/ MajorDescription' 和 ''在這裏建議How does one use SimpleDOM sortedXPath to sort on node value?但是當我用var_dump()檢查它時,所有的東西都會返回一個空的數組我認爲問題是我需要對子節點的值進行排序 - 這可能嗎? foreach需要在ProgramList上,因爲我需要在每次迭代時輸出ProgramList中所有子節點的值。
有什麼建議嗎?我不必使用SimpleDOM,我可以使用任何可用的方法 - 目前我正在迭代AZ數組,迭代ProgramList,將MajorDescription的第一個字母與當前字母進行比較,然後對每個字母進行迭代輸出,如果它匹配 - 這顯然是不是的理想,只有第一個字母排序...
謝謝 - 你的答案激勵我回頭看看usort - 我無法完全用你的代碼工作,但它通過使用這個問題的答案使用usort工作http://stackoverflow.com/questions/8274193/排序XML-DIV逐子節點-PHP-的SimpleXML – tsdexter