0

I have an SimpleXMLElement Object:

 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 
        ) 
    )
)

Now I want to sort the Childs ("one", "two" "three") by the attribute "position". How can I solve this problem?

Thanks!

Deenadhayalan Manoharan
  • 5,436
  • 14
  • 30
  • 50
  • Hi, have a look here. You are going to have to use `array_multisort` function http://php.net/manual/en/function.array-multisort.php – Schalk Keun Mar 20 '15 at 08:09

1 Answers1

-1

Use this function:

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;
}

To use it do this:

sortByKeyValue(SimpleXMLElementObject, 'position')
NaijaProgrammer
  • 2,892
  • 2
  • 24
  • 33