PHP订单SimpleXMLElement

时间:2015-03-20 08:05:53

标签: php arrays xml object simplexml

我有一个SimpleXMLElement对象:

 SimpleXMLElement Object ( 
    [@attributes] => Array ( 
        [id] => 1234 
        [color] => red 
    ) 
    [one] => SimpleXMLElement Object ( 
        [@attributes] => Array ( 
            [position] => 2 
            [close] => false 
        ) 
    ) 
    [two] => SimpleXMLElement Object ( 
        [@attributes] => Array ( 
            [position] => 0 
            [close] => false 
        ) 
    ) 
    [three] => SimpleXMLElement Object ( 
        [@attributes] => Array ( 
            [position] => 0 
            [close] => true 
        ) 
    )
)

现在我想通过属性" position"对Childs(" one"," two"" three")进行排序。 我该如何解决这个问题?

谢谢!

1 个答案:

答案 0 :(得分:-1)

使用此功能:

function sortByKeyValue($the_array, $key)
{
   $cmp_val="((\$a['$key']>\$b['$key']) ? 1 : ((\$a['$key']==\$b['$key']) 0:-1))";
   $cmp=create_function('$a, $b', "return $cmp_val;");
   uasort($the_array, $cmp);

   return $the_array;
}

要使用它,请执行以下操作:

sortByKeyValue(SimpleXMLElementObject, 'position')