Can anyone make a recommendation for sorting a xml tree with a structure as given:

   [@attributes] => Array
        (
            [version] => 4.2.1
        )

[action] => getclientsproducts
[result] => success
[clientid] => 000000
[totalresults] => 28
[products] => SimpleXMLElement Object
    (
        [product] => Array
            (
                [0] => SimpleXMLElement Object
                                    ...
                                    [status] => Active
                                    ...
                [1] => SimpleXMLElement Object
                                    ...
                                    [status] => Cancelled
                                    ... 
            )
    )

so i have 28 products inside there (i only showed 2)... the status can be one of six states (active, canceled, pending, terminated, suspended, fraud) ... I'd like to resort the xml order inside [product] such that active items are always first and the rest are last (if I can pass a reference for the sort order for each state that would be ideal but not mandatory)

any suggestions would be appreciated =)

    scrupul0us,

    You can parse the

    $activeArray = array();
    $restOfIt = array();
    $count = 0;
    foreach ($simpleXMLObject->products as $productObject) {
       if ($productObject->status == 'Active') {
          $activeArray[$count] =  $productObject;
       } else {
          $restOfIt[$count] = $productObject;
       }
       $count++;
    }
    $finalArray = array_merge($activeArray,$restOfIt);
    $simpleXMLObject->products = $finalArray;
    

    .. just a thought.. of course, its just kinda thrown together here while I wait for someone here @ work to get their stuff together.

    Let me know what you think...

      i actually took a different approach as I didnt have time to figure out sorting...

      I parsed each product and ran a switch structure against that status state and just .= a variable for each state

      in the end I just appended each variable together in the order I needed and ta-da i'm in business

      never-the-less something I'd like to know how to do =)

        scrupul0us,

        I'd love to see some sample data & your execution if you will. Still seems like a fun challenge never the less.

          Write a Reply...