あなたはこのような多くの要素
$string = <<<EOS
<page>
<talentTrees>
<tree name="Football" order="2" />
<tree name="Baseball" order="0" />
<tree name="Frisbee" order="1" />
</talentTrees>
<talentTrees>
<tree name="Football2" order="1" />
<tree name="Baseball2" order="2" />
<tree name="Frisbee2" order="0" />
</talentTrees>
</page>
EOS;
を持っている場合あなたがforeachを使用することができます。
$xml = simplexml_load_string($string);
function sort_trees($t1, $t2) {
return $t1['order'] - $t2['order'];
}
foreach($xml->talentTrees as $talentTrees){
foreach($talentTrees->tree as $tree){
$trees[]= $tree;
}
usort($trees, 'sort_trees');
print_r($trees);
unset($trees);
}
出力:
別の例として
Array
(
[0] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => Baseball
[order] => 0
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => Frisbee
[order] => 1
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => Football
[order] => 2
)
)
)
[1] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => Frisbee2
[order] => 0
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => Football2
[order] => 1
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => Baseball2
[order] => 2
)
)
)
)
:https://stackoverflow.com/a/44379495/3506219この数字は9よりも大きい場合にはこのようにソートされます失敗
:1、100 2、245、300、4の代わりに1、2、4、100、下記のJosh Davisの提案を使用してください。それはうまくいく。 – matthoiland