【问题标题】:PHP order SimpleXMLElement [duplicate]PHP命令SimpleXMLElement [重复]
【发布时间】:2015-03-20 16:32:16
【问题描述】:

我有一个 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 
        ) 
    )
)

现在我想按属性“位置”对孩子(“一”、“二”、“三”)进行排序。 我该如何解决这个问题?

谢谢!

【问题讨论】:

标签: php arrays xml object simplexml


【解决方案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')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    相关资源
    最近更新 更多